深入理解VUE组件

淩亂°似流年 2023-03-12 09:12 51阅读 0赞

组件参数校验(props:{})

**props:**可以规范的参数类型为
单个参数:直接写参数类型
复数参数:数组形式 [ ] ,将每个类型添加进去
对象型参数
第一个值为,参数类型( type: String)
type包括
String
Number
Boolean
Array
Object
Date
Function
Symbol

第二个值为,参数是否必须(required: true),true为必须,false为非必须,即如果没有传递 propC 参数,就会报错
第三个值为,默认值(default: 100),即如果没有传递参数过来,会自动调用默认的值
validator
可以自定义校验器,对参数进行检查校验

  1. props: {
  2. // 基础的类型检查 (`null` 和 `undefined` 会通过任何类型验证)
  3. propA: Number,
  4. // 多个可能的类型
  5. propB: [String, Number],
  6. // 必填的字符串
  7. propC: {
  8. type: String,
  9. required: true
  10. },
  11. // 带有默认值的数字
  12. propD: {
  13. type: Number,
  14. default: 100
  15. },
  16. // 带有默认值的对象
  17. propE: {
  18. type: Object,
  19. // 对象或数组默认值必须从一个工厂函数获取
  20. default: function () {
  21. return { message: 'hello' }
  22. }
  23. },
  24. // 自定义验证函数
  25. propF: {
  26. validator: function (value) {
  27. // 这个值必须匹配下列字符串中的一个
  28. return ['success', 'warning', 'danger'].indexOf(value) !== -1
  29. }
  30. }

给组件绑定原生事件(.native修饰符)

你可能有很多次想要在一个组件的根元素上直接监听一个原生事件。
这时,你可以使用 v-on.native 修饰符:

  1. <div id="root">
  2. <child-components @click="foo"></child-components>
  3. //在父组件上绑定的事件,是无法被触发的
  4. //<child-components @click.native="foo"></child-components>
  5. // 在 事件 type 上加上 .native 的修饰符(如:@click.native),就可以触发了
  6. </div>
  7. <script>
  8. var childComponents = {
  9. template:"<div>childComponents</div>",
  10. };
  11. var vm = new Vue({
  12. el:"#root",
  13. components:{
  14. childComponents
  15. },
  16. methods: {
  17. foo:function(){
  18. console.log("123");
  19. }
  20. },
  21. })
  22. </script>

非父子组件之间传值

插槽(slot)

插槽的使用为,父组件 —> 子组件(slot)
即 父组件 向 子组件 传递内容,子组件用 slot 插槽进行接收

  1. <div id="root">
  2. <child-com>
  3. <p>hello</p>
  4. </child-com>
  5. </div>
  6. var childCom = {
  7. template:
  8. `<div> <slot></slot> <p>world</p> </div>`,
  9. }

具名插槽(slot name=” “)可以将父组件内容传递到指定插槽内部

  1. <child-com>
  2. <p slot="hello">hello</p>
  3. <p slot="world">world</p>
  4. </child-com>
  5. var childCom = {
  6. template:
  7. `<div> <slot name="hello"></slot> <p>bey</p> <slot name="world"></slot> </div>`,
  8. }

作用域插槽

让(父组件)插槽 内容 能够访问 子组件中才有的 数据 是很有用的

动态组件与v-once指令

动态组件语法(必须要 omponent 标签包裹)
currentTabComponent 参数 的意思是,需要切换的组件名

  1. <component v-bind:is="currentTabComponent"></component>

可以点击 change 按钮 来回切换 childconone 组件 与 childcontwo 组件

  1. <div id="root">
  2. <component :is="type"></component>
  3. <!-- <childconone v-if="type === 'one'"></childconone>
  4. <childcontwo v-if="type === 'two'"></childcontwo> -->
  5. <button @click="btnClick">change</button>
  6. </div>
  7. var childconone = {
  8. template:"<div>child-one</div>",
  9. data() {
  10. return {
  11. }
  12. },
  13. }
  14. var childcontwo = {
  15. template:"<div>child-two</div>",
  16. data() {
  17. return {
  18. }
  19. },
  20. }
  21. methods: {
  22. btnClick:function(){
  23. this.type = this.type === "childconone" ? "childcontwo" : "childconone";
  24. }
  25. },

动态组件小demo,顺便一提 keep-alive 的使用 keep-alive 会保持这些组件的状态

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="vue.js"></script>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. list-style-type: none;
  13. }
  14. .bord{
  15. border: 1px solid black;
  16. }
  17. .one{
  18. height: 65px;
  19. width: 300px;
  20. text-align: left;
  21. }
  22. .two{
  23. height: 20px;
  24. width: 300px;
  25. text-align: left;
  26. }
  27. .ulbtn{
  28. float: left;
  29. display: inline-block;
  30. border-right: 1px solid grey;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="root">
  36. <button @click="pageo">page1</button><button @click="paget">page2</button>
  37. <keep-alive>
  38. <component :is="type"></component>
  39. </keep-alive>
  40. </div>
  41. <script>
  42. var one = {
  43. template:`<div class="bord one"> <ul class="ulbtn"> <li @click="lione">1-1</li> <li @click="litwo">1-2</li> <li @click="lithree">1-3</li> </ul> <div v-if="type === 'on'"> 默认页 </div> <div v-if="type === 'one'"> 第一页 </div> <div v-if="type === 'two'"> 第二页 </div> <div v-if="type === 'three'"> 第三页 </div> </div>`,
  44. data() {
  45. return {
  46. type:"on"
  47. }
  48. },
  49. methods:{
  50. lione:function(){
  51. this.type = "one"
  52. },
  53. litwo:function(){
  54. this.type = "two"
  55. },
  56. lithree:function(){
  57. this.type = "three"
  58. },
  59. }
  60. }
  61. var two = {
  62. template:`<div class="bord two">page2</div>`,
  63. }
  64. Vue.component("one-one",{
  65. template:"",
  66. data() {
  67. return {
  68. }
  69. },
  70. })
  71. var vm = new Vue({
  72. el:"#root",
  73. data() {
  74. return {
  75. type:"one",
  76. }
  77. },
  78. components:{
  79. one,
  80. two
  81. },
  82. methods: {
  83. pageo:function(){
  84. this.type = "one"
  85. },
  86. paget:function(){
  87. this.type = "two"
  88. }
  89. },
  90. })
  91. </script>
  92. </body>
  93. </html>

v-once指令
写在根,id= “root”,的最外层的 div 上
意思是,就算组件中的内容有所改变,我也不会改变,只会在加载的时候渲染一次
即,外层 div上有 v-once ,其内部内容有所改变,也不会重新渲染内部

发表评论

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

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

相关阅读

    相关 深入vue

    1.组件的引入 在组件声明的驼峰式命名的标签,被渲染到HTML里时,会改成-小写的形式(2.0以下版本),命名的时候要以-小写的形式命名。 ![20180501131909

    相关 Vue

    Vue组件 1. 什么是组件 1. 组件的概念:组件即自定义控件,是Vue.js最强大的功能之一 2. 组件的用途:组件能够封装可重用代码,扩展html标签功能