Ashun's 技術駅 Ashun's 技術駅
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Ashun

前端界的小学生
首页
  • 前端文章

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • Vue
  • 现代web布局
  • React
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 技术资源
  • 第一阶段

    • HTML
  • 第二阶段

    • JavaScript
  • 第三阶段

    • Vue
  • 第四阶段

    • 实战项目
  • 每周测试

    • 每周
  • 其他

    • Vue引入UI框架
    • Web前端面试
    • Vue3-resource
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 福利资源
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 01mvvm和mvc
  • 02安装和使用
  • 03模版语法
  • 04生命周期
  • 05指令
  • 06方法-计算属性-监听器
  • 07class和style绑定
  • 08条件渲染
  • 09列表渲染
  • 10事件处理
  • 11表单输入绑定
  • 12组件
  • 13组件之间传递数据
  • 14props验证
  • 15插槽
    • 创建插槽
    • 具名插槽
    • 默认插槽
    • 作用域插槽
    • 案例插槽完整版
  • 16动态组件
  • 17处理边界情况
  • 18脚手架
  • 19Vuex
  • 20VueRouter
  • 21自定义指令
  • 22渲染函数
  • 23插件
  • 24VueConfig
  • 《Vue》
xugaoyi
2022-02-14
目录

15插槽

# 插槽

# 创建插槽

<mycomponent>
  内容
</mycomponent>

let mycomponent = {
  template:
    "<div><slot></slot></div>",
};

//根组件
new Vue({
  el: "#app",
  components: {
    mycomponent,
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

%accordion% 创建插槽练习🚀 %accordion%

<div id="app">
    <my-component1>
        我是一个插槽
    </my-component1>
</div>

<script>
    //局部组件(区域注册)子组件
    let myComponent1 = {
        //slot:插槽
        template: "<div><slot></slot></div>",

    };

    //父组件
    new Vue({
        el: "#app",
        data: {
        },
        components: {
            myComponent1,
        },
    });
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

%/accordion%

# 具名插槽

<mycomponent>
  <div slot='a'>内容1</div>
  <div slot='b'>内容2</div>
</mycomponent>

let mycomponent = {
  template:
    "<div><slot name='a'></slot>内容x<slot name='b'></slot></div>",
};

//根组件
new Vue({
  el: "#app",
  components: {
    mycomponent,
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

%accordion% 具名插槽 ——就给他起名字🚀 %accordion%

<div id="app">
    <my-component1>
        <div slot="a">我是插槽1</div>
        <div slot="b">我是插槽2</div>

    </my-component1>
</div>

<script>
    //局部组件(区域注册)子组件
    let myComponent1 = {
        //slot:插槽
        template: "<div><slot name='a'></slot> ==楚河汉界== <slot name='b'></slot></div>",

    };

    //父组件
    new Vue({
        el: "#app",
        data: {
        },
        components: {
            myComponent1,
        },
    });
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

打印出来的效果

我是插槽1
==楚河汉界==
我是插槽2
1
2
3

%/accordion%

# 默认插槽

<mycomponent>
  <div>内容1</div>
  <div slot='b'>内容2</div>
</mycomponent>

let mycomponent = {
  template:
    "<div><slot name='default'></slot>内容x<slot name='b'></slot></div>",
};

//根组件
new Vue({
  el: "#app",
  components: {
    mycomponent,
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 作用域插槽

使用v-slot可以提取出当前插槽的数据

<mycomponent :arr="arr">
  <template v-slot="{row,idx}">
    <button @click='show(row,idx)'>查看</button>
  </template>
</mycomponent>

let mycomponent = {
  props:{
    arr:{
      type:Array,
      required:true
    }
  },
  template: `
    <table border='1' cellspacing='0'>
      <tr>
        <td>商品名称</td>
        <td>商品数量</td>
        <td>商品价钱</td>
        <td>操作</td>     
      </tr>
      <tr v-for="(item,idx) of arr">
        <td>{{item.name}}</td>
        <td>{{item.num}}</td>
        <td>{{item.price}}</td>
        <td>
          <slot :row='item' :idx="idx"></slot>
        </td>     
      </tr>
    </table>
    `,
};

//根组件
new Vue({
  el: "#app",
  components: {
    mycomponent,
  },
  data: {
    arr: [
      { name: "苹果13", num: 1, price: 1000 },
      { name: "苹果14", num: 2, price: 2000 },
      { name: "苹果15", num: 3, price: 3000 },
    ],
  },
  methods: {
    show(row,idx){
      console.log(row)
      console.log(idx)
    }
  },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

%accordion% 作用域插槽练习🚀 %accordion%

<div id="app">
    <my-component1>
        <!-- 使用v-slot可以提取出当前插槽的数据 -->
        <template v-slot="{data}">🐱‍🐉插槽{{data}}</template>
    </my-component1>
</div>

<script>
    //局部组件(区域注册)子组件
    let myComponent1 = {
        //slot:插槽
        template: "<div><slot :data='msg'></slot></div>",

        data() {
            return {
                msg: ",👀作用域"
            }
        }
    };

    //父组件
    new Vue({
        el: "#app",
        data: {
        },
        components: {
            myComponent1,
        },
    });
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

打印出来的效果

🐱‍🐉插槽,👀作用域
1

%/accordion%

作用域插槽里面使用具名插槽

<mycomponent :arr="arr">
  <template v-slot:a="{row,idx}">
    <button @click='show(row,idx)'>查看</button>
  </template>
  <template v-slot:b="{row,idx}">
    <button @click='show(row,idx)'>编辑</button>
  </template>
</mycomponent>

let mycomponent = {
  props:{
    arr:{
      type:Array,
      required:true
    }
  },
  template: `
    <table border='1' cellspacing='0'>
      <tr>
        <td>商品名称</td>
        <td>商品数量</td>
        <td>商品价钱</td>
        <td>操作</td>     
      </tr>
      <tr v-for="(item,idx) of arr">
        <td>{{item.name}}</td>
        <td>{{item.num}}</td>
        <td>{{item.price}}</td>
        <td>
          <slot name='a' :row='item' :idx="idx"></slot>
          <slot name='b' :row='item' :idx="idx"></slot>
        </td>     
      </tr>
    </table>
    `,
};

//根组件
new Vue({
  el: "#app",
  components: {
    mycomponent,
  },
  data: {
    arr: [
      { name: "苹果13", num: 1, price: 1000 },
      { name: "苹果14", num: 2, price: 2000 },
      { name: "苹果15", num: 3, price: 3000 },
    ],
  },
  methods: {
    show(row,idx){
      console.log(row)
      console.log(idx)
    }
  },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

# 案例插槽完整版

//作用域插槽 ——就给他起名字1️⃣
<div id="app">
    <my-component1 :list="goodsList">
        //使用v-slot可以提取出当前插槽的数据
        <template v-slot:a>
            <h1>第一个表格</h1>
        </template>
        <template v-slot:b="{row,idx}">
            <button @click="show(row,idx)">查看</button>
        </template>
    </my-component1>

    <my-component1 :list="goodsList1">
        //使用v-slot可以提取出当前插槽的数据
        <template v-slot:a>
            <h1>第二个表格</h1>
        </template>
        <template v-slot:b="{row,idx}">
            <button @click="show(row,idx)">编辑</button>
        </template>
    </my-component1>
</div>

<script>
    //局部组件(区域注册)子组件2️⃣
    let myComponent1 = {
        props: {
            list: {
                type: Array,
                required: true,
            },
        },
        //slot:插槽
        template: `
        <div>
            <slot name='a'></slot>
            <table border="1" cellspacing="0">
                <tr v-for='(item,idx) of list' :key='idx'>
                    <td>{{item.name}}</td>
                    <td>{{item.price}}</td>
                    <td>
                        <slot name='b' :row="item" :idx='idx'></slot>
                    </td>
                </tr>
            </table>
        </div>
        `,
    };

    //父组件3️⃣
    new Vue({
        el: "#app",
        data: {
            goodsList: [
                {
                    name: "🍎苹果",
                    price: 100,
                },
                {
                    name: "🍍菠萝",
                    price: 200,
                },
                {
                    name: "🍌香蕉",
                    price: 300,
                },
            ],
            goodsList1: [
                {
                    name: "🍉西瓜",
                    price: 100,
                },
                {
                    name: "🍒樱桃",
                    price: 200,
                },
                {
                    name: "🥝猕猴桃",
                    price: 300,
                },
            ],
        },
        components: {
            myComponent1,
        },
        methods: {
            show(row, idx) {
                console.log(idx);
            },
        },
    });
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

image-20220112125337760

编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41
14props验证
16动态组件

← 14props验证 16动态组件→

最近更新
01
课件-react路由-V6
01-22
02
课件-国际化
01-22
03
课件-redux-toolkit
01-22
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式