Vue 基础篇二

摘要:
Vue组件组件是Vue.js最强大的功能之一。组件可以扩展HTML元素,封装可重用代码,是一个可重用的Vue实例。组件注册//html代码//js代码Vue。component('my component',{template:'component!'})varapp=newVue;全局注册//html code//div˃//js code//组件中的数据必须是varChild={template:'Acomponent!

Vue 组件

组件(Component)是Vue.js最强大的功能之一.

组件可以扩展HTML元素,封装可重用的代码,是可复用的Vue实例.

组件的注册

Vue 基础篇二第1张Vue 基础篇二第2张
// html 代码
<div id="app">
  <my-component></my-component>
</div>
// js 代码
Vue.component('my-component', {
  template: '<div>A component!</div>'
})
var app = new Vue({
  el: '#app',
  data: {
       
  } 
});
全局注册
Vue 基础篇二第3张Vue 基础篇二第4张
// html 代码
<div id="app">
  <my-component></my-component>
</div>
// js 代码
// 组件中的data必须是个函数
var Child = {
  template: '<div>A component!</div>',
  data: function() {
      return {
            name: "gao",
      }
}};

new Vue({
  // ...
  components: {
    // <my-component> 将只在父组件模板中可用
    'my-component': Child
  }
})
局部注册
Vue 基础篇二第5张Vue 基础篇二第6张
// js 代码
Vue.component('child', {
    template: `<div><button @click="on_click()">{{msg}}</button></div>`,
    data: function () {
        return {
            msg: "点我",
        }
    },
    methods: {
        on_click(){
            alert(123)
        }
    }
});
new Vue({
    el: "#app",
})
组件中的data methods
Vue 基础篇二第7张Vue 基础篇二第8张
<script>
    var my_component = {
        template: `<div><h1>{{msg}}</h1></div>`,
        data(){
            return{
                msg: "这是子组件"
            }
        }
    };
    var global_component = {
        template: `<div>
                        <h1>{{msg}}</h1>
                        <button @click="on_click">点我</button>
                        <my_component></my_component>
                    </div>`,
        data(){
            return {
                msg: "全局组件"
            }
        },
        methods: {
            on_click() {
                alert("123")
            }
        },
        components:{
            my_component:my_component,
        }
    };
    const app = new Vue({
        el: "#app",
        data: {

        },
        components: {
            global_component: global_component,
            // my_component: my_component,
        }
    });


</script>
子组件的注册

组件之间的通信

我们的组件在任何地方用的时候都要一个样子么

可不可以我们给组件传个参数~让组件在不同的地方表现不同的状态

我们之前说过的博客评论@某某某,点击用户名可以跳转到该用户站点

遮掩过一个小功能,我们每次@的时候都要写,我们可以封装组件,传值即可

Vue 基础篇二第9张Vue 基础篇二第10张
// html 代码
<div id="app">
    <child username="gaoxin"></child>
</div>
// js 代码
Vue.component('child', {
    template: `<a :href="'/user/'+ username">{{username}}</a>`,
    props: ["username"],
});


var app = new Vue({
    el: "#app",
    data:{
        name: "@gaoxin"
    }

});
父子通信

app.$on(event,callback)监听当前实例上的自定义事件,事件由$emit触发,回调函数接收事件触发器额外参数.

app.$emit(event,[args.....])触发当前实例上的事件,额外参数传给监听器的callback回调函数.

Vue 基础篇二第11张Vue 基础篇二第12张
// html 代码
<div id="app">
    <parent></parent>
</div>
// js 代码
Vue.component('parent',{
    template: `
        <div>
            <child @show_balance="show"></child>
            <p v-if="active">您的余额998</p>
        </div>
    `,
    data: function () {
        return {
            active: false,
        }
    },
    methods: {
        show: function(data){
            this.active=true;
            console.log(data)
        }
    }

});
Vue.component('child', {
    template: `<div><button @click="on_click()">{{msg}}</button></div>`,
    data: function () {
        return {
            msg: "显示余额",
        }
    },
    methods: {
        on_click(){
            // alert(123)
            this.$emit('show_balance', {q:1,b:2})
        }
    }
});
子父通信

平行组件之间的通信,喊话需要一个中间调度器,在组件加载完成之后去监听调度器事件,回调函数接收数据.

Vue 基础篇二第13张Vue 基础篇二第14张
// html 代码
<div id="app">
    <whh></whh>
    <shh></shh>
</div>
// js 代码
var Event = new Vue()

Vue.component('whh',{
    template: `
        <div>
            我说: <input @keyup="on_change" v-model="i_said">
        </div>
    `,
    data: function () {
        return {
            i_said: '',
        }
    },
    methods: {
        on_change: function () {
            Event.$emit("whh_said_something", this.i_said)
        }
    }
});
Vue.component('shh', {
    template: `
        <div>
            花花说:{{whh_said}}
        </div>
    `,
    data: function () {
        return {
            whh_said: '',
        }
    },
    mounted: function () {
        var me = this
        Event.$on('whh_said_something', function (data) {
            me.whh_said = data
        })
    }
});
非父子组件通信

混合Mixins

重复功能和数据的存储器,可以覆盖Mixins的内容

Vue 基础篇二第15张Vue 基础篇二第16张
// 点击显示和隐藏  提示框的显示和隐藏
// html 代码
<div id="app">
    <PopUp></PopUp>
    <ToolTip></ToolTip>
</div>
// js 代码
var base = {
     data: function () {
        return {
            visible: false,
        }
    },
    methods: {
        show: function () {
            this.visible = true
        },
        hide: function () {
            this.visible = false
        }
    }
}

Vue.component('popup', {
    template:`
        <div>
        <button @click="show">PopUp show</button>
        <button @click="hide">PopUp hide</button>
        <div v-if="visible"><p>hello everybody</p></div>
        </div>
    `,
    mixins: [base],
    data: function () {
        return {
            visible: true,
        }
    }

});
Vue.component('tooltip', {
    template: `
        <div>
        <div @mouseenter="show" @mouseleave="hide">ToolTip</div>
        <div v-if="visible"><p>ToolTip</p></div>
        </div>
    `,
    mixins: [base]
});

new Vue({
    el: "#app",
})
Mixins

插槽 Slot

插槽是一套内容分发的API,在组件中,<slot>作为内容承载分发的出口

Vue 基础篇二第17张Vue 基础篇二第18张
// html 代码
<div id="app">
    <panel>
        <div slot="title"> HELLO</div>
        <div slot="content">hello</div>
        
    </panel>
    <panel></panel>
    <panel></panel>
</div>

<template id="panel-tpl">
    <div class="panel">
        <div class="title">
            <slot name="title"></slot>
        </div>
        <div class="content">
            <slot name="content"></slot>
        </div>
        <!--<div class="content">Failure is probably the fortification in your pole. It is like a peek your wallet as the thief, when you are thinking how to spend several hard-won lepta,</div>-->
        <div class="footer">
            <slot name="footer">更多信息</slot>
        </div>
    </div>
</template>
// js 代码
Vue.component('panel', {
    template: '#panel-tpl',

});

new Vue({
    el: "#app",
})
Slot

免责声明:文章转载自《Vue 基础篇二》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Tomcat无法重新安装该怎么办?关于窗口重画的初级问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

vue props 用法(转载)

前面的话   组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项。在 Vue 中,父子组件的关系可以总结为 props down, events up。父组件通过 props 向下传递数据给子组件,子组件通过 events 给父组件发送消息。本文将详细介绍Vue组件选项props 父子级组件   在介绍props之前,先介绍...

vue 高德地图 地图初始化显示接口返回的多个经纬度

npm 安装 依赖。 npm install vue-amap --save mian.js 中注册import VueAMap from 'vue-amap';Vue.use(VueAMap);VueAMap.initAMapApiLoader({key: '高德的key',//高德的keyplugin: ['AMap.Autocomplete', 'A...

[vue/no-parsing-error] Parsing error: invalid-first-character-of-tag-name.eslint-plugin-vue Parsing error: invalid-first-character-of-tag-name.eslint(vue/no-parsing-error)

问题描述: vue页面中使用插值语法和三元表达式,出现以上报错,但是可以正常运行; 报错代码如: <p>{{num<0?"你好":"hello"}}</> 报错信息 [vue/no-parsing-error] Parsing error: invalid-first-character-of-tag-name.eslin...

VSCode 启动 Vue 项目 npm install 报错

1. 报错后,查看了版本。 查看node版本:node -v 查看npm版本:npm -v 查看Augular版本:ng --version 2. 感觉 Augular CLI版本太低,使用以下方法升级到最新版本: 》 npm uninstall -g @angular/cli 》 npm cache verify (或 npm cache clean -...

【Vue后台管理二】vue-admin-template 对接后端API JWT认证

初始化对接端台API 上一篇了完成了项目初始化,但是那个只是把 vue-admin-template 模版简单的初始化了一下,新增了tagsview标签快捷导航栏,其他的没什么变化。 这一篇了就完成了和后端的Jwt token认证,登录,退出,基本的table list接口数据请求。首先看看效果。 其实看起来和第一篇的初始化效果差不多,唯一的区别是,第一篇...

VUE+Flask登录的初探--前端(Vue+element+axios)+后端(Flask+FlaskLogin+JWT)

0.前端部分依然基于VueCLI (https://cli.vuejs.org/zh/) 1.创建hello-login文件夹,然后再此文件夹内执行 vue create front-end ,一顿狂回车后,如下图所示:  2.安装elementUI,axios,js-cookie,qs  2.1  npm i element-ui -S  (https...