100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > vue 组件 全局组件和局部组件component

vue 组件 全局组件和局部组件component

时间:2021-02-05 12:13:48

相关推荐

vue 组件 全局组件和局部组件component

1、全局组件

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><div id="app">vue组件测试</div><script src="/npm/vue/dist/vue.js"></script><script type="text/javascript">//注册全局组件 ponent('my-component',{template: '<div>我是全局组件</div>'})ponent('other-component', {template: '<div>我是另一个全局组件<my-component></my-component></div>'})new Vue({el:"#app",template:'<other-component></other-component>'})</script></body></html>

显示结果:

我是另一个全局组件

我是全局组件

2、局部组件

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><div id="app">vue组件测试</div><script src="/npm/vue/dist/vue.js"></script><script type="text/javascript">//注册局部组件 const child = {template: '<div>我是局部组件</div>'}new Vue({el:"#app",template:'<my-component></my-component>',components:{'my-component': child}})</script></body></html>

显示结果:

我是局部组件

3、组件嵌套

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title></head><body><div id="app">vue组件测试</div><script src="/npm/vue/dist/vue.js"></script><script type="text/javascript">//注册局部组件 const child = {template: '<div>我是局部child组件</div>'}const father = {template: '<div>我是father组件,我包含了:<child-component></child-component> </div>',components: {'child-component': child}}const grandFather={template: '<div>我是grandFather组件,我包含了:<father-component></father-component> </div>',components: {'father-component': father}}new Vue({el:"#app",template:'<my-component></my-component>',components:{'my-component': grandFather}})</script></body></html>

显示结果:

我是grandFather组件,我包含了:

我是father组件,我包含了:

我是局部child组件

参考自:/penghuwan/p/7222106.html#_label0

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。