vue组件化之父子通信、注册组件

以你之姓@ 2023-02-18 14:07 81阅读 0赞

1、我在hbuildx新建项目,vue的基本模板

巴拉巴拉等待中。。。。

下载下来的目录结构如下:
vue基本结构

2、开始书写组件:src目录下的components目录下写.vue文件:father.vue,child.vue(默认的HellowWorld.vue可以使用,也可以不用)

child.vue的代码如下:

  1. <template>
  2. <div id="child">
  3. <div class="blog-post">
  4. <h3>{ { post.title }}</h3>
  5. <button @click="tabs">
  6. Enlarge text{ { count}}
  7. </button>
  8. <button :disabled="post.disabled" @click="count+=3">count进步3</button>
  9. <div v-html="post.content"></div>
  10. <div v-for="(item,index) in post.arr" :key="index">{ { item.name}}</div>
  11. <div>{ { size}}</div>
  12. <slot></slot>//插槽,可以在父组件自定义内容
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'child',
  19. props: {
  20. post: {
  21. type: Object,
  22. default: function() {
  23. return { }
  24. }
  25. },
  26. /* count:{//count定义在这里会报错 type:Number, default:1 } */
  27. },
  28. data: function() {
  29. return {
  30. count: 1,
  31. size:'ABC'
  32. }
  33. },
  34. computed: {
  35. normalSize: function() {
  36. return this.size.trim().toLowerCase();
  37. }
  38. },
  39. methods: {
  40. tabs: function() {
  41. this.count++;
  42. this.size = this.getUpperCharacter();
  43. this.$emit('clicknow', [this.count, this.normalSize])
  44. },
  45. getCharacter: function(flag) {
  46. var character = [];
  47. if (flag === "lower") {
  48. character = String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0));
  49. }
  50. if (flag === "upper") {
  51. for(var i = 0; i<3;i++){
  52. character.push(String.fromCharCode(Math.floor(Math.random() * 26) + "A".charCodeAt(0)));
  53. }
  54. }
  55. return character.toString().replace(/,/g,'');
  56. },
  57. getUpperCharacter: function() {
  58. return this.getCharacter("upper");
  59. },
  60. getLowerCharacter: function() {
  61. return this.getCharacter("lower");
  62. }
  63. }
  64. }
  65. </script>
  66. <style>
  67. </style>

father.vue的代码如下:

  1. <template>
  2. <div id="hello">
  3. <Child v-for="post in posts" v-bind:key="post.id" v-bind:post="post" @clicknow="tabb">
  4. { { coun}}
  5. </Child>
  6. </div>
  7. </template>
  8. <script>
  9. import Child from './Child.vue'
  10. export default {
  11. name: 'hello',
  12. components: {
  13. Child
  14. },
  15. data: function() {
  16. return {
  17. posts: [{
  18. id: 1,
  19. content: '你好',
  20. title: '标题',
  21. disabled: true,
  22. arr:[{
  23. name:'love'
  24. }]
  25. }],
  26. coun: 1,
  27. }
  28. },
  29. mounted() {
  30. },
  31. methods: {
  32. tabb: function(e) {
  33. this.coun = e[0];
  34. console.log(e[1])
  35. },
  36. }
  37. }
  38. </script>
  39. <style>
  40. </style>

App.vue的代码如下:

  1. <template>
  2. <div id="app">
  3. <Father></Father>
  4. </div>
  5. </template>
  6. <script>
  7. import Father from './components/Father.vue'
  8. export default {
  9. name: 'app',
  10. components: {
  11. Father
  12. },
  13. methods: {
  14. }
  15. }
  16. </script>
  17. <style>
  18. </style>

3、 如何在father中使用child.vue?

在父组件中使用子组件

4、如何将father和child渲染出来呢?

  1. 在father.vue中使用child,需要引入child组件:引入方式为import Child from ./child.vue,使用方式见以上代码
  2. 需要在app.vue引入father.vue

本节重点:

1、子组件使用prop或者data函数定义变量
2、单向下行绑定:使用prop定义的变量不可以再子组件中做运算,即不可在子组件中改变变量的值,否则控制台会发出警告;同时注意一下:prop定义的数组或者对象是可以改变父组件的值的,因为—》在 JavaScript 中对象和数组是通过引用传入的,所以对于一个数组或对象类型的 prop 来说,在子组件中改变变更这个对象或数组本身将会影响到父组件的状态。
3、父组件改变变量的状态和值会直接影响子组件,反之,控制台会发出警告。

全局的方式注册组件

如下:

1、 优点:全局注册的组件在工程项目中任意地方都可以使用:即–》全局注册的组件可以用在其被注册之后的任何 (通过 new Vue) 新创建的 Vue 根实例,也包括其组件树中的所有子组件的模板中。
2、 缺点:开发人员在打包时会把不必要甚至没有使用过的组件会一直在构建的项目中,即–》如果你使用一个像 webpack这样的构建系统,全局注册所有的组件意味着即便你已经不再使用一个组件了,它仍然会被包含在你最终的构建结果中。这造成了用户下载的JavaScript 的无谓的增加

局部的方式注册:

本次项目例子就是局部注册的。哪里要使用,先引入才可以。

  1. var ComponentA = { /* ... */ }
  2. var ComponentB = { /* ... */ }
  3. var ComponentC = { /* ... */ }
  4. new Vue({
  5. el: '#app',
  6. components: {
  7. 'component-a': ComponentA,
  8. 'component-b': ComponentB
  9. }
  10. })

对于 components 对象中的每个 property 来说,其 property 名就是自定义元素的名字,其 property 值就是这个组件的选项对象。

注意局部注册的组件在其子组件中不可用。例如,如果你希望 ComponentA 在 ComponentB 中可用,则你需要这样写:

  1. var ComponentA = { /* ... */ }
  2. var ComponentB = {
  3. components: {
  4. 'component-a': ComponentA
  5. },
  6. // ...
  7. }

或者如果你通过 Babel 和 webpack 使用 ES2015 模块,那么代码看起来更像:

  1. import ComponentA from './ComponentA.vue'
  2. export default {
  3. components: {
  4. ComponentA
  5. },
  6. // ...
  7. }

ComponentA是ComponentA:ComponentA的缩写

  1. 用在模板中的自定义元素的名称
  2. 包含了这个组件选项的变量名




    / 全局注册 /

    1. Vue.component('Counter',{
    2. data(){ return{ count:10}},
    3. template:`<div>{ {count}}</div>`
    4. })
    5. /* 局部注册 */
    6. var Coun = {
    7. data(){ return{ coun:20}},
    8. template:`<div>{ {coun}}</div>`
    9. }
    10. var vm =new Vue({
    11. el:'#app',
    12. data:function(){
    13. return{
    14. }
    15. },
    16. components:{
    17. Coun//局部注册的
    18. }
    19. })

更多推荐
vue基础

发表评论

表情:
评论列表 (有 0 条评论,81人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue父子组件传值

    一、前言 作为国内使用较多的前端框架——Vue,作为一名开发人员是必须要掌握的,小编作为一名后端人员。由于公司前端人员不足,也是学起来了Vue! 框架的精髓就在于,组件