Vue_(组件通讯)组件

  Vue组件  传送门

  组件Component,可扩展HTML元素,封装可重用的代码。通俗的来说,组件将可重用的HTML元素封装成为标签方便复用;

  组件的使用:

  使用全局方法Vue.extend创建构造器;

  再使用全局方法Vue.component注册组件;

  在Vue.component里需要指明组件的名称,组件名称不能使用内置标签名称,如body

  推荐使用短横线命名规则。例:

    my-component 正确 (推荐)

    my-Component 错误

    mycomponent 正确

    Mycomponent 正确

    myComponent 错误

    MyComponent 错误

  Learn

    一、组件注册 

    二、全局组件与局部组件

    三、this is component-a

  目录结构

  

  【每个demo下方都存有html源码】

一、组件注册  传送门

  第一种方法:使用构造器注册组件

        <div id="GaryId">
            <!--<h1>hello Vue</h1>-->
            <my-component></my-component>
        </div>
            //创建构造器
            let myComponent =  Vue.extend({
                template:"<h1>hello Vue</h1>"
            })

            //注册组件
            Vue.component(‘my-component‘,myComponent);

  第二种方法:组件的简写

        <div id="GaryId">
            <!--<h1>hello Vue</h1>-->
            <my-componenta></my-componenta>
        </div>
            //注册组件的简写
            Vue.component(‘my-componenta‘,{
                template:"<h2>hello Gary</h2>"
            })

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <!--<h1>hello Vue</h1>-->
            <my-component></my-component>
            <my-componenta></my-componenta>
        </div>
    </body>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

            //创建构造器
            let myComponent =  Vue.extend({
                template:"<h1>hello Vue</h1>"
            })

            //注册组件
            Vue.component(‘my-component‘,myComponent);

            //注册组件的简写
            Vue.component(‘my-componenta‘,{
                template:"<h2>hello Gary</h2>"
            })

            new Vue({
                data:{
                    msg:‘Gary‘
                }
            }).$mount("#GaryId");
    </script>
</html>

Gary_component.html

 二、全局组件与局部组件  传送门

  组件可分为全局组件与局部组件;

  全局组件:

    在全局API中的Vue.component注册

    该项目中所有Vue实例内均可以使用

  局部组件:

    在Vue实例中的components选项中注册

    只能在本实例中使用

  全局组件和局部组件都可以存储数据,但是存储的方式与Vue实例中的data稍有不同;

  组件里存储数据,data后需加上函数,数据写在函数体中

  this is component-a作为局部属性使用

局部组件:只可以再div id="GaryId"中使用

        <div id="GaryId">
            <my-component-a></my-component-a>
        </div>
            new Vue({
                data:{
                    msg:‘Gary‘
                },
                components:{
                    "my-component-a":{
                        template:"<h2>this is component-a</h2>"
                    }
                }
            }).$mount("#GaryId");

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <my-component-a></my-component-a>
        </div>
    </body>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

            new Vue({
                data:{
                    msg:‘Gary‘
                },
                components:{
                    "my-component-a":{
                        template:"<h2>this is component-a</h2>"
                    }
                }
            }).$mount("#GaryId");

    </script>
</html>

Gary_component-02.html

全局属性:可以在任意div中调用

        <div id="GaryId">
            <my-component-a></my-component-a>
            <my-component-b></my-component-b>
        </div>
            //注册组件的简写(默认全局)
            Vue.component(‘my-component-b‘,{
                template:"<h2>{{name}}</h2>",
                data:function(){
                    return {
                        name:‘this is component-b‘
                    }
                }
            })

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <my-component-a></my-component-a>
            <my-component-b></my-component-b>
        </div>
    </body>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

            //注册组件的简写(默认全局)
            Vue.component(‘my-component-b‘,{
                template:"<h2>{{name}}</h2>",
                data:function(){
                    return {
                        name:‘this is component-b‘
                    }
                }
            })

            new Vue({
                data:{
                    msg:‘Gary‘
                },
                components:{
                    "my-component-a":{
                        template:"<h2>this is component-a</h2>"
                    }
                }
            }).$mount("#GaryId");

    </script>
</html>

Gary_component-02.html

三、this is component-a

  在component的template中书写大量的HTML元素很麻烦

  Vue提供了<template>标签,可以在里边书写HTML,然后通过ID指定到组建内的template属性上;

  <body>标签中调用组件

        <div id="GaryId">
            <my-component-b></my-component-b>
        </div>

  在<template>标签中通过id"my-template"添加组件

        <template id="my-template">
        <div>
            <h2>{{name}}</h2>
            <button @click="count++">{{count}}</button>
            <ul>
                <li v-for="item in numArray">{{item}}</li>
            </ul>
        </div>
    </template>
new Vue({
            data : {
                msg : ‘123‘
            },
            components : {
                "my-component-b" : {
                    template :  "#my-template",
                    data(){
                        return {
                            name : "my-component-b !!",
                            numArray : [1, 2, 3, 4, 5],
                            count : 0
                        }
                    }
                }
            }
            }).$mount("#GaryId");

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Gary</title>
    </head>
    <body>
        <div id="GaryId">
            <my-component-b></my-component-b>
        </div>
    </body>

        <template id="my-template">
        <div>
            <h2>{{name}}</h2>
            <button @click="count++">{{count}}</button>
            <ul>
                <li v-for="item in numArray">{{item}}</li>
            </ul>
        </div>
    </template>

    <script type="text/javascript" src="../js/vue.js" ></script>
    <script type="text/javascript">

        new Vue({
            data : {
                msg : ‘123‘
            },
            components : {
                "my-component-b" : {
                    template :  "#my-template",
                    data(){
                        return {
                            name : "my-component-b !!",
                            numArray : [1, 2, 3, 4, 5],
                            count : 0
                        }
                    }
                }
            }
            }).$mount("#GaryId");

    </script>
</html>

Gary_component-03.html

原文地址:https://www.cnblogs.com/1138720556Gary/p/10446719.html

时间: 2024-08-30 12:39:59

Vue_(组件通讯)组件的相关文章

路由与组件通讯

路由的钩子:(即导航守卫) 1.全局的, const router = new VueRouter({ ... }) router.beforeEach((to, from, next) => { // ... }) 2.单个路由独享的 const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, beforeEnter: (to, from, next) => { // ... } } ] }) 3.组件级的

突破自我,开源NetWorkSocket通讯组件

前言 在<化茧成蝶,开源NetWorkSocket通讯组件>发表之后,收到大家很多个star,在此感谢!更可贵的是,一些网友提出了许多好建议,经过一些时间的思考,决定将NetworkSocket从模仿MVC之后,继续模仿Owin,并局部突破,作有自己特色和理念的通讯框架.之所以模仿Owin而不是接入,是因为Open Web Interface for .NET是为Web而存在的,我需要的是应该是Open Tcp Interface for .NET,当然这是还不存在的标准,但我想达到或接近这种

SignalR循序渐进(三)简易的集群通讯组件

上一篇演示了泛型Hub的实现,微软于6月17日更新了SignalR 2.1.0,然后自带了泛型Hub,于是就不需要自己去实现了…(微软你为啥不早一个月自带啊…).不过没关系,SignalR出彩之处不在泛型Hub,本篇为各位观众带来了基于SignalR的简易集群通讯组件Demo,可用于分布式定时任务. 说到集群,自然想到了NLB啊Cluster啊HPC啊等等.NLB受制于成员数量,Cluster用数量堆高可用性,HPC太复杂.本着SignalR的双向异步通讯的特点,其实是可以用来玩弹性计算的.初始

.Net Core开源通讯组件 SmartRoute(服务即集群)

SmartRoute是基于Dotnet Core设计的可运行在linux和windows下的服务通讯组件,其设计理念是去中心化和零配置即可实现服务通讯集群.SmartRoute是通过消息订阅的机制实现服务与服务之间的通讯,它可以让广播网段内所有服务器上的应用自动构建通讯集群: 而通讯集群完全是SmartRoute自动构建并不需要进行任何配置或安装中间服务.通过这种全新的通讯开发方式可以让开发者更轻松和简单地构建基于服务的集群通讯应用. SmartRoute的发展目标   智能集成服务通讯交互 不

手机浏览器,QQ通讯组件无法唤醒QQ客户端

昨天遇到了这个问题,在网页端可以正常使用QQ通讯组件,但是放到了移动端就不行.试了好久终于搞定,在此分享一下我的经验,如果这让你少走了弯路,请给我点个赞吧. 要使用QQ通讯组件,首先百度QQ互联,点击后出现如下界面: 接着,点击功能组件. 我们只需要把代码中链接部分放到程序相应的地方即可,不必全部使用. 本来这样就可以实现QQ通讯的功能,在网页端已经试过了,可以通讯,但在手机浏览器却总会得到这样的提示: 我的QQ版本已经是最新的,可还是会提示,点击打开链接也只是让你下载QQ应用而已,并不能唤醒手

openstack组件通讯端口定义

openstack 组件通讯是通过ZeroMQ+ceilometer发送组件调用信息,具体是通过TCP通讯,发送数据和接收数据是用同一个端口(在配置文件指定),下面通过代码稍作解析: IceHouse/ceilometer/ceilometer/openstack/common/rpc/impl_zmq.py def _multi_send(method, context, topic, msg, timeout=None, envelope=False, _msg_id=None): "&qu

NetworkSocket C# 网络通讯 组件

本项目已迁移到 https://github.com/xljiulang/NetworkSocket 这是.net4.0下一个高性能.高可扩展性的Tcp异步通讯组件,内部基于SocketAsyncEventArgs封装,发送和接收完全分离,用户可基于底层的NetworkSocket.dll实现某种协议的高效服务器和客户端,也可以直接使用NetworkSocket.Fast.dll来快速实现自己的通讯服务项目:另外,解决方案提供了NetworkSocket.WebSocket.dll已实现了web

Angular1组件通讯方式总结

这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angular1.4及以前,主要是基于HTML的,将所有view划分为不同的HTML片段,通过路由,transclude,include等方式,按照用户行为切换显示不同界面.对于每个template内部来讲,可以是纯HTML,也可以是自定义的directive.directive之间可以有层级关系,也可以没有层级关系.在Angular1

vue2.0中eventBus实现兄弟组件通讯

我们知道,在vue中父子组件的通讯是通过props和自定义事件搞定的,简单那的非父子组件通讯用bus(一个空的Vue实例),针对中大型的项目会选择vuex,然而小项目的话,便捷的解决方案就是eventBus. 官网相关描述在:$dispatch和$broadcast替换  一节中.提到: $dispatch 和 $broadcast 也没有解决兄弟组件间的通信问题.对于$dispatch 和 $broadcast最简单的升级方式就是:通过使用事件中心,允许组件自由交流,无论组件处于组件树的哪一层