vue CDN 模板挂载操作

创建模板

在html 页面 body标签内添加一下模板
注意:模板ID要唯一

 	<template id='tableDataTemplate'>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="date" label="日期" width="180">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="180">
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>
        </el-table>
    </template>

全局引用

全局注册组件

<script>
    Vue.component('todo-item', {
        // todo-item 组件现在接受一个
        // "prop",类似于一个自定义 attribute。
        // 这个 prop 名为 todo。
        props: ['todo'],
        template: '#tableDataTemplate',
        data() {
            return {
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }]
            }
        },
        methods: {
            demo: function (val) {
                console.log('调用的子模块的方法',val);
            }
        },
        mounted(){
            console.log('组件被挂载了');
        }
    })
 </script>

vue 模板引用

 <script>
    new Vue({
        el: '#app',
        data: function () {
            return { visible: false }
        },
        components: {
           // tableDataTemplate
        },
        mounted() {
            window.vue = this;
        },
        // template: '#tableDataTemplate',
    })
 </script>

完整代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<body>
    <div id="app">
        <el-button @click="visible = true">Button</el-button>
        <el-dialog :visible.sync="visible" title="Hello world">
            <p>Try Element</p>
        </el-dialog>
        <todo-item ref="reference"></todo-item>
    </div>

    <template id='tableDataTemplate'>
        <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="date" label="日期" width="180">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="180">
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>
        </el-table>
    </template>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
    Vue.component('todo-item', {
        // todo-item 组件现在接受一个
        // "prop",类似于一个自定义 attribute。
        // 这个 prop 名为 todo。
        props: ['todo'],
        template: '#tableDataTemplate',
        data() {
            return {
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }]
            }
        },
        methods: {
            demo: function (val) {
                console.log('调用的子模块的方法',val);
            }
        },
        mounted(){
            console.log('组件被挂载了');
        }
    })
    new Vue({
        el: '#app',
        data: function () {
            return { visible: false }
        },
        components: {
           // tableDataTemplate
        },
        mounted() {
            window.vue = this;
        },
        // template: '#tableDataTemplate',
    })
</script>

</html>

运行结果

在这里插入图片描述

局部引用

局部注册组件

<script>
    var ComponentA = {
        // todo-item 组件现在接受一个
        // "prop",类似于一个自定义 attribute。
        // 这个 prop 名为 todo。
        props: ['todo'],
        template: '#tableDataTemplate',
        data() {
            return {
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }]
            }
        },
        methods: {
            demo: function (val) {
                console.log('调用的子模块的方法', val);
            },
            sendMessage: function () {
                console.log(this.$emit);
                this.$emit('sendaa', '组件ComponentA发送消息了');//发送消息给父组件
            }
        },
        mounted() {
            console.log('组件被挂载了');
        }
    };
</script>

vue 模板引用

<script>
    new Vue({
        el: '#app',
        data: function () {
            return { visible: false, msg: '' }
        },
        components: {
            'component-a': ComponentA
        },
        mounted() {
            window.vue = this;
        },
        methods: {
            sendaa: function (val) {
               this.msg = val;//监听子组件发送的消息
            }
        }
    })
</script>

完整代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<body>
    <div id="app">
        <el-button @click="visible = true">Button</el-button>
        <el-dialog :visible.sync="visible" title="Hello world">
            <p>Try Element</p>
        </el-dialog>
        接收到子组件的信息:{{msg}}
        <component-a ref="reference" @sendaa="sendaa"></component-a>
    </div>

    <template id='tableDataTemplate'>
        <div>
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="date" label="日期" width="180">
                </el-table-column>
                <el-table-column prop="name" label="姓名" width="180">
                </el-table-column>
                <el-table-column prop="address" label="地址">
                </el-table-column>
            </el-table>
            <el-button type="primary" @click='sendMessage'>发送消息给父组件</el-button>
        </div>
    </template>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
    var ComponentA = {
        // todo-item 组件现在接受一个
        // "prop",类似于一个自定义 attribute。
        // 这个 prop 名为 todo。
        props: ['todo'],
        template: '#tableDataTemplate',
        data() {
            return {
                tableData: [{
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄'
                }, {
                    date: '2016-05-04',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1517 弄'
                }, {
                    date: '2016-05-01',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1519 弄'
                }, {
                    date: '2016-05-03',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1516 弄'
                }]
            }
        },
        methods: {
            demo: function (val) {
                console.log('调用的子模块的方法', val);
            },
            sendMessage: function () {
                console.log(this.$emit);
                this.$emit('sendaa', '组件ComponentA发送消息了');
            }
        },
        mounted() {
            console.log('组件被挂载了');
        }
    };
    new Vue({
        el: '#app',
        data: function () {
            return { visible: false, msg: '' }
        },
        components: {
            'component-a': ComponentA
        },
        mounted() {
            window.vue = this;
        },
        methods: {
            sendaa: function (val) {
               this.msg = val;
            }
        }
    })
</script>

</html>

运行结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yweir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值