Vue组件模板及组件互相引用代码实例
更新时间:2020年03月11日 12:50:46 作者:我太难了008
这篇文章主要介绍了Vue组件模板及组件互相引用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1. vue组件都是由这三部分组成
<template> <div> </div> </template> <script> export default{} </script> <style> </style>
2. 组件间的引用
分3步走,假设现在有两个组件 App.vue,和 Add.vue,现在要把Add.vue组件引入到App.vue组件中
App.vue
<template> // 第3步 <Add/> </template> <script> // 第1步 import Add from './components/Add.vue' // 第2步 components: { Add } } </script> <style> </style>
3. 组件间数据的传递
假将要将App.vue组件中的数据传递到Ad.vue组件中
App.vue
<template> // 第3步 // 传递数据,注意冒号 <Add :comments="comments"/> </template> <script> // 第1步 import Add from './components/Add.vue' // 第2步 components: { Add }, // App组件中初始化的数据 data(){ return { comments: [{ name: 'Bob', content: 'Vue 还不错' }, { name: 'Cat', content: 'Vue so Easy' }, { name: 'BZ', content: 'Vue so so' } ] } } } </script> <style> </style>
Add.vue
<script> export default{ // 声明接收comments数据 props: ['comments'] } </script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
vue this.$router和this.$route区别解析及路由传参的2种方式 && this.$route
this.$router 相当于一个全局的路由器对象,包含了很多属性和对象(比如 history 对象),任何页面都可以调用其 push(), replace(), go() 等方法,本文给大家介绍Vue中this.$router与this.$route的区别 及push()方法,感兴趣的朋友跟随小编一起看看吧2023-10-10
最新评论