原文:https://www.jianshu.com/p/bb18f738179f
父子组件使用
在工程目录/src下创建component文件夹,并在component文件夹下创建一个 firstcomponent.vue并写仿照 App.vue 的格式写一个组件。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<template> <div id="firstcomponent"> <h1>I am a title</h1> <a> {{ author }} </a> </div> </template> <script type="text/javascript"> export default { data () { return { author: "第一个子组件" } } } </script> <style> </style>

image
然后在 App.vue 使用组件
第一步,引入。在<script></script>标签内的第一行写:
复制代码
1
2import firstcomponent from './component/firstcomponent.vue'
第二步,注册。在<script></script>标签内的 data 代码块后面加上 components: { firstcomponent }
复制代码
1
2
3
4
5
6
7
8
9export default { data () { return { msg: 'Hello Vue!' } }, components: { firstcomponent } }
第三步,使用。在<template></template>内加上<firstcomponent></firstcomponent>
复制代码
1
2
3
4
5
6
7
8<template> <div id="app"> <img src="./assets/logo.png"> <h1>{{ msg }}</h1> <firstcomponent></firstcomponent> </div> </template>
全局组件
- 使用更为方便,不需要声明,直接用
- 在main.js中引入一次并使用
Vue.component('组件名', 组件对象)
- 所有的组件通过组件名使用
最后
以上就是高高草丛最近收集整理的关于vue为p标签_[转]Vue组件的引用的全部内容,更多相关vue为p标签_[转]Vue组件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复