Vue 组件与复用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Vue 组件与复用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3010字,纯文字阅读大概需要5分钟。
内容图文
(1)全局注册
<!
DOCTYPE html
>
<
html
lang
="zh"
>
<
head
>
<
meta
charset
="UTF-8"
/>
<
title
>Vue</title></head><body><div id="app"><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注册 Vue.component(‘my-component‘, {
//DOM结构必须被元素包含 template: ‘<div>组件内容</div>‘
})
new Vue({
el: "#app"
})
</script></body></html>
(2)局部注册
<!
DOCTYPE html
>
<
html
lang
="zh"
>
<
head
>
<
meta
charset
="UTF-8"
/>
<
title
>Vue</title></head><body><div id="app"><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">var Child = {
template: ‘<div>组件内容</div>‘
}
new Vue({
el: "#app",
components: {
‘my-component‘: Child
}
})
</script></body></html>
(3)is挂载组件
table、ul、ol、select这些标签会限制其内的元素,这时可以使用is来挂载组件
<!
DOCTYPE html
>
<
html
lang
="zh"
>
<
head
>
<
meta
charset
="UTF-8"
/>
<
title
>Vue</title></head><body><div id="app"><table><tbody is=‘my-component‘></tbody></table></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注册 Vue.component(‘my-component‘, {
//DOM结构必须被元素包含 template: ‘<div>组件内容</div>‘
})
new Vue({
el: "#app"
})
</script></body></html>
(4)组件也可以有data,method,computed等属性。但是data是函数,数据需要return出去。
<!
DOCTYPE html
>
<
html
lang
="zh"
>
<
head
>
<
meta
charset
="UTF-8"
/>
<
title
>Vue</title></head><body><div id="app"><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注册 Vue.component(‘my-component‘, {
//DOM结构必须被元素包含 template: ‘<div>{{message}}</div>‘,
data: function() {
return {
message: ‘组件内容‘
}
}
})
new Vue({
el: "#app"
})
</script></body></html>
(5)解决多个组件之间数据共享问题
给组件返回一个新的data对象
<!
DOCTYPE html
>
<
html
lang
="zh"
>
<
head
>
<
meta
charset
="UTF-8"
/>
<
title
>Vue</title></head><body><div id="app"><my-component></my-component><my-component></my-component><my-component></my-component></div><script src="https://cdn.bootcss.com/vue/2.5.9/vue.min.js"></script><script type="text/javascript">//示例前注册 Vue.component(‘my-component‘, {
//DOM结构必须被元素包含 template: ‘<button @click="counter++">{{counter}}</button>‘,
data: function() {
return {
counter: 0
}
}
})
new Vue({
el: "#app"
})
</script></body></html>
原文:http://www.cnblogs.com/mengfangui/p/8056865.html
内容总结
以上是互联网集市为您收集整理的Vue 组件与复用全部内容,希望文章能够帮你解决Vue 组件与复用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。