const cpnC = Vue.extend({
template: `
我是标题
我是内容1
我是内容2
`
});
Vue.component('my-cpn',cpnC);
<my-cpn>my-cpn>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<title>组件基本使用title>
<style>
style>
head>
<body>
<div id="app">
<my-cpn>my-cpn>
div>
<script>
// 创建组件构造器对象
const cpnC = Vue.extend({
template: `
我是标题
我是内容
`
});
// 注册组件
Vue.component('my-cpn',cpnC);
const app = new Vue({
el: '#app',
data: {
}
})
script>
body>
html>
const cpnC = Vue.extend({
template: `
局部组件
局部组件内容
`
});
const app = new Vue({
el: '#app',
components: {
'my-cpn': cpnC
}
});
<my-cpn>my-cpn>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<title>组件基本使用title>
<style>
style>
head>
<body>
<div id="app">
<my-cpn>my-cpn>
div>
<script>
// 创建组件构造器对象
const cpnC = Vue.extend({
template: `
我是标题
我是内容
`
});
// 注册局部组件
const app = new Vue({
el: '#app',
components: {
'my-cpn':cpnC
}
})
script>
body>
html>
// 子组件构造器实例
const cpnC1 = Vue.extend({
template: `
子组件
子组件内容
`
});
// 父组件构造器实例
const cpnC2 = Vue.extend({
template: `
父组件
父组件内容
`,
components: { // 父组件中注册子组件
cpn1: cpnC1
}
});
const app = new Vue({
el: '#app',
components: {
cpn2: cpnC2
}
})
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<title>父子组件title>
<style>
style>
head>
<body>
<div id="app">
<cpn2>cpn2>
div>
<script>
// 子组件构造器实例
const cpnC1 = Vue.extend({
template: `
子组件
子组件内容
`
});
// 父组件构造器实例
const cpnC2 = Vue.extend({
template: `
父组件
父组件内容
`,
components: { // 父组件中注册子组件
cpn1: cpnC1
}
});
const app = new Vue({
el: '#app',
components: {
cpn2: cpnC2
}
})
script>
body>
html>
组件模板分离写法,提供两种方式,分别为使用script标签和template标签
text/x-template
<script type="text/x-template" id="myCpn">
<div>
<h2>我是组件1</h2>
<p>组件1内容</p>
</div>
script>
<template id="myCpn2">
<div>
<h2>我是组件2h2>
<p>组件2内容p>
div>
template>
Vue.component('cpn1',{tmplate: '#myCpn'})
Vue.component('cpn2',{tmplate: '#myCpn2'})
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<title>Documenttitle>
<style>
style>
head>
<body>
<div id="app">
<cpn1>cpn1>
<cpn2>cpn2>
div>
<script type="text/x-template" id="myCnp">
<div>
<h2>我是组件1</h2>
<p>组件1内容</p>
</div>
script>
<template id="myCnp2">
<div>
<h2>我是组件2h2>
<p>组件2内容p>
div>
template>
<script>
Vue.component('cpn1',{template: '#myCnp'})
Vue.component('cpn2',{template: '#myCnp2'})
const app = new Vue({
el: '#app',
data: {
}
})
script>
body>
html>