这篇文章将为大家详细讲解有关Vue中组件的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
<body>
<div id="root">
<h3>{{name}}</h3>
<hr>
<school></school>
<hr>
<student></student>
<hr>
<!-- <h3>学生姓名:{{name}}</h3>
<h3>学生年龄:{{age}}</h3> -->
</div>
<div id="root2">
<hello></hello>
</div>
<script>
Vue.config.productionTip = false;
//创建school组件
//el:'#root'
//组件定义时 一定不要写el配置项,因为最终所有的组件都要被一个vm管理 由vm决定服务于哪个容器
const school = Vue.extend({
template: `
<div>
<h3>学校名称:{{schoolName}}</h3>
<h3>学校地址:{{address}}</h3>
<button @click="showName">点我提示学校名称</button>
</div>
`,
data() {
return {
schoolName: '二中',
address: '北京',
}
},
methods: {
showName() {
alert(this.schoolName)
}
}
})
//第一步:创建组件
//创建学生组件
const student = Vue.extend({
template: `
<div>
<h3>学生姓名:{{name}}</h3>
<h3>学生年龄:{{age}}</h3>
</div>
`,
data() {
return {
name: '小王',
age: 20,
}
}
})
//创建vm
new Vue({
el: '#root',
data: {
name: '你好,世界!'
},
//第二步:注册组件(局部注册)
components: {
school,
student
}
})
const hello = Vue.extend({
template: `
<div><h3>你好鸭!王同学</h3></div>
`
})
Vue.component('hello', hello)
new Vue({
el: '#root2'
})
</script>
</body>
关于“Vue中组件的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。