[关闭]
@Wangww0925 2020-08-05T07:37:38.000000Z 字数 2746 阅读 278

Vue CLI 引入组件

Vue_CLI


调用外部组件

引用组件

  1. <template>
  2. <div>
  3. <p>引入外部组件</p>
  4. <test /> <!-- 2、使用组件 -->
  5. </div>
  6. </template>
  7. <script>
  8. import test from "./module.vue"; // 1、引入组件
  9. export default{
  10. components:{
  11. test // 2、注册组件
  12. // test:test // es5写法, 前一个test指标签命名,后一个指引入组件命名
  13. }
  14. }
  15. </script>
  16. <style scoped>
  17. p{
  18. margin-top: 50px;
  19. font-size: 24px;
  20. font-weight: 700;
  21. }
  22. </style>

module.vue

  1. <template>
  2. <div class="main">
  3. 组件内容
  4. </div>
  5. </template>
  6. <style scoped>
  7. .main{
  8. width: 500px;
  9. height: 150px;
  10. line-height: 150px;
  11. margin: 50px auto;
  12. font-size: 18px;
  13. border: 1px solid #eee;
  14. background-color: pink;
  15. color: #fff;
  16. }
  17. </style>

image_1eeubie7k9461e1e21u1caf4l4p.png-16.4kB


组件数据传递 props

注意:

  1. 1、属性命名为from-here时,模板要使用小驼峰式写法调用及引用,所以建立少用`-`
  2. 2、如需向组件中传递data中的数据,那么我们在绑定时,使用`v-bind:here1="message"` `:here1="message"`

引用组件

  1. <template>
  2. <div class="main">
  3. <p>组件数据传递</p>
  4. <test here="China" from-here="中国" :here1="message" /> <!-- 2、使用组件 -->
  5. </div>
  6. </template>
  7. <script>
  8. import test from "./module.vue"; // 1、引入组件
  9. export default{
  10. data(){
  11. return {
  12. message: '浙江'
  13. }
  14. },
  15. components:{
  16. test // 2、注册组件
  17. }
  18. }
  19. </script>
  20. <style>
  21. .main{
  22. padding: 50px;
  23. text-align: left;
  24. }
  25. .main > p{
  26. font-weight: 700;
  27. font-size: 18px;
  28. }
  29. </style>

module.vue

  1. <template>
  2. <div>
  3. <p>写法一:panda 来自 {{here}}</p>
  4. <p>写法二:我来自{{fromHere}} </p>
  5. <p>写法二:我来自{{here1}}</p>
  6. </div>
  7. </template>
  8. <script>
  9. export default{
  10. props: ['here','fromHere','here1']
  11. }
  12. </script>

image_1eeuc1aoigs1ju2162tpcb50j1m.png-9.8kB


子组件向父组件传递数据 $emit

引用组件

  1. <template>
  2. <div class="main">
  3. <p>子组件向父组件传递数据</p>
  4. <test v-on:getMsg="childInfo" /> <!-- 2、使用组件,定义接收数据方法 -->
  5. <div>展示父组件获取子组件内容: {{info}}</div>
  6. </div>
  7. </template>
  8. <script>
  9. import test from "./module.vue"; // 1、引入组件
  10. export default{
  11. components:{
  12. test // 2、注册组件
  13. },
  14. data(){
  15. return{
  16. info: ''
  17. }
  18. },
  19. methods:{
  20. // 接收子组件数据
  21. childInfo(val){
  22. this.info = val; // www
  23. }
  24. },
  25. }
  26. </script>
  27. <style>
  28. .main{
  29. padding: 50px;
  30. text-align: left;
  31. }
  32. .main > p{
  33. font-weight: 700;
  34. font-size: 18px;
  35. }
  36. </style>

module.vue

  1. <template>
  2. <div>
  3. 子组件内容: {{msg}}
  4. <button @click="change">点击向父组件传值($emit)</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default{
  9. data(){
  10. return {
  11. msg:"www"
  12. }
  13. },
  14. methods:{
  15. change(){
  16. this.$emit("getMsg", this.msg); // 向父组件传递数据
  17. }
  18. }
  19. }
  20. </script>
  21. <style scoped>
  22. div{
  23. margin: 30px 0px;
  24. }
  25. </style>

效果图
image_1eeuiq7ma13m8i351o5dcld164km.png-18.1kB

image_1eeuinnm11v5m1cpv1io64f4fuj9.png-15kB


动态绑定我们的组件,根据数据的不同更换不同的组件

引用组件

  1. <template>
  2. <div class="main">
  3. <p>子组件向父组件传递数据</p>
  4. <component :is="who"></component> <!-- 3、使用component标签,根据who的值不同,调用不同的组件 -->
  5. <button @click="change">更换component组件内容</button> <!-- 4、给页面加个按钮,更换组件 -->
  6. </div>
  7. </template>
  8. <script>
  9. // 1、引入组件
  10. import test1 from "./module1.vue";
  11. import test2 from "./module2.vue";
  12. import test3 from "./module3.vue";
  13. export default{
  14. // 2、注册组件
  15. components:{
  16. test1,
  17. test2,
  18. test3
  19. },
  20. data(){
  21. return{
  22. who: 'test1'
  23. }
  24. },
  25. methods:{
  26. change(){
  27. if(this.who == "test1"){
  28. this.who = "test2"
  29. }else if(this.who == "test2"){
  30. this.who = "test3"
  31. }else{
  32. this.who = "test1"
  33. }
  34. }
  35. },
  36. }
  37. </script>
  38. <style>
  39. .main{
  40. padding: 50px;
  41. text-align: left;
  42. }
  43. .main > p{
  44. font-weight: 700;
  45. font-size: 18px;
  46. }
  47. div{
  48. margin: 30px 0px;
  49. }
  50. </style>

module1.vue

  1. <template>
  2. <div>
  3. module1内容
  4. </div>
  5. </template>

module2.vue

  1. <template>
  2. <div>
  3. module2内容
  4. </div>
  5. </template>

module3.vue

  1. <template>
  2. <div>
  3. module3内容
  4. </div>
  5. </template>

image_1eeujm4da1c8psapenm1vqg1qo5m.png-11.7kB

image_1eeujmc0q1f22fggflb1g4dmui13.png-11.9kB

image_1eeujlodfv6h1jif1a5munli0u9.png-11kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注