vue基础内容{通信,注册,路由,组件}

ES6常用语法

-- 变量

-- var 变量提升

-- let  {}

-- const 常量

-- 模板字符串

-- ``

-- ${}

-- 函数

-- 箭头函数

-- this

-- 普通函数取决于函数最近的调用者

-- 箭头函数取决于当前上下文环境

-- 数据的解构

-- 注意语法

-- 类

-- class 定义类

-- extends 继承

-- constructor 构造方法

-- 子类是没有this的 用super()

-- export import

-- export {}  ---》 import {} from .....

-- export default  一个文件只能有一个  import xxxx from xx

Vue的常用指令

-- v-text  innerText

-- v-html  innerHtml

-- v-if    appendChlid

-- v-for   循环

-- v-show  display

-- v-bind  绑定属性 :   <div :class="{"类名": is_show}"></div>

-- v-on    绑定事件的   所有的js的事件

-- v-model 数据双向绑定

-- input

-- select

-- textarea

-- 指令修饰符

-- .lazy  不要实时绑定 懒

-- .trim  去空格

-- .number  转换成数字

-- 自定义的指令

-- Vue.directive("指令名称",function(el, binding){

el: 绑定指令的标签

binding: 指令的所有信息

})

-- 计算属性   {{num}}

-- computed: {

num: function(){

return xxxx

}

}

-- 放入缓存 只有数据发生改变的时候才会重新计算

-- 获取DOM

-- 给标签添加ref属性

<div ref="my_box"></div>

-- this.$refs.my_box

-- 数据监听

-- watch: {

my_data: {

handler: function(val, oldVal){

val 新的值

oldVal 旧的值

注意 数组跟对象新旧值是一样的

}

deep: true

}

}

-- 数组

-- 长度改变的时候才会被监听到

-- 改变数组的值的时候深度监听监听不到

-- app.$set(array, index, value)

-- 对象

-- 深度监听是可以监听到的

Vue的组件

-- 组件的注册

-- 全局注册

Vue.component("组件名称",{

template只能识别一个作用域块

template: `<div></div>`,

data(){

return {

xxx: xxx

}

},

methods: {},

.....

})

<组件名称></组件名称>

-- 局部注册

-- let my_com = {

template只能识别一个作用域块

template: `<div></div>`,

data(){

return {

xxx: xxx

}

},

methods: {},

.....

}

-- 注册在Vue的实例化对象里

const app = new Vue({

el: "#app",

components: {

my_com: my_com

}

})

-- <my_com></my_com>

-- 子组件的注册

-- 在父组件里写compontents: {

类比局部组件注册

}

-- 在父组件的template里显示子组件对应的标签

<clild></child>

-- 组件之间的通信

-- 父子通信

-- 给父组件里的子组件绑定属性

<child :属性名称="父亲向儿子说的话"></child>

-- 子组件通过props拿到数据

props:[属性名称]

-- 子父通信

-- 子组件要提交事件

this.$emit("事件名称", 儿子向父亲说的话)

-- 给父组件里的子组件绑定事件

<child @事件名称="自己处理的事件名称"></child>

methods: {

自己处理的事件名称: function(){}

}

-- 非父子通信

-- 定义一个中间调度器

const Event = new Vue()

-- 其中一个组件向中间调度器提交事件

Event.$emit("事件名称", data)

-- 另一个组件监听调度器的事件

mounted(){

Event.$on("事件名称", function(data){

do something

注意this

})

}

-- 混合

-- 作用代码的复用

-- let base = {

可复用的代码块

}

-- mixins: [base]

-- 可以重写base里的内容

-- 插槽

-- <my_com></my_com>不支持标签里写内容

-- slot

-- 命名的插槽

<slot name="xxx"></slot>

<div slot="xxx"></div>

Vue的路由

-- 路由的注册

-- let url = [

{

path: "/",

component: {

template: ``

},

},

{

path: "/course",

component: {

template: ``

},

}

]

-- let router = new VueRouter({

routes: url

})

-- const app = new Vue({

el: "#app",

router: router

})

--  <router-link to="/">首页</router-link>

<router-link to="/course">课程页面</router-link>

<router-view></router-view>

-- 子路由

-- let url = [

{

path: "/",

component: {

template: `写子路由的router-link以及router-view

<router-link to=""></router-link>

`

},

children: [

{

path: "/user",

component: {

template: ``

}

}

]

}

]

-- 命名的路由

-- let url = [

{

path: "/",

name: "home",

component: {

template: ``

},

},

{

path: "/course",

component: {

template: ``

},

}

]

-- let router = new VueRouter({

routes: url,

去掉路由上的#

mode: "history"

})

-- const app = new Vue({

el: "#app",

router: router

})

--  <router-link :to="{name: ‘home‘}">首页</router-link>

<router-link to="/course">课程页面</router-link>

<router-view></router-view>

-- 路由的参数

-- -- let url = [

{

path: "/",

component: {

template: ``

},

},

{

path: "/course/:course_name",

component: {

template: ``

},

}

]

-- this.$route

路由的所有信息

fullpath

path

query  ?后的数据

params  是路由里的变量

meta

-- this.$router

VueRouter实例化对象

-- 手动路由

-- this.$router.push("/")

-- this.$router.push({name: "xxx", params: {key:value}, query: {key:value}})

-- 路由的钩子

-- router.beforeEach(function(to, from, next){

to 你要去哪

from 哪里来的

next 下一步干嘛

})

-- router.afterEach(function(to, from){})

-- 命名的路由视图

-- 当一个路由对应多个组件的时候

-- <router-view name="对应组件的名称"></router-view>

原文地址:https://www.cnblogs.com/cz007/p/9960982.html

时间: 2024-08-26 14:55:05

vue基础内容{通信,注册,路由,组件}的相关文章

5.0 路由组件的使用

一.概述 路由器:路由器管理路由; 路由:路由是一种映射关系,一个key对应一个value,key是path,对于后台路由,     value是处理请求的回调函数,对于前台路由value是组件. 说明:1) 官方提供的用来实现 SPA(单个页面) 的 vue 插件2) github: https://github.com/vuejs/vue-router3) 中文文档: http://router.vuejs.org/zh-cn/4) 下载: npm install vue-router --

vue基础----组件通信(props,$emit,$attrs,$listeners)

一.父传子,子传孙 1. props 1>在父组件中通过子组件自定义的标签属性来传递数据. 2>在子组件中通过props声明希望用到的数据 1 <body> 2 <div id="app"> 3 <my-father :msg1="msg" a="10" :b="20" @click="fn"></my-father> 4 </div>

vue基础----组件通信($parent,$children)

1.按照dom的父子级关系,在子组件中可以通过$parent 直接调用父组件的方法,也可得到父组件的属性. 2.在父组件中通过$childrens可以得到一个子组件数组,能够在父组件中调用子组件的方法和得到属性. 3.特别注意一下_uid标识每一个组件. 下面是一个下拉菜单举例 1 <body> 2 <div id="app"> 3 <collapse> 4 <collapse-item title="大学同学">大学

Vue基础篇--8组件基础 component

Vue基础篇--8组件基础 component 1.简单示例 <div id='components1'> <button-conter></button-conter> </div> <script> // 定义一个名为button-conter组件 Vue.component("button-conter",{ data:function () { return { count:0 } }, template:`<b

vue基础篇---vue组件《2》

定义全局组件 我们通过Vue的component方法来定义一个全局组件. <div id="app"> <!--使用定义好的全局组件--> <counter></counter> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script type="text/javascript&quo

Vue基础(环境配置、内部指令、全局API、选项、内置组件)

1.环境配置 安装VsCode 安装包管理工具:直接下载 NodeJS 进行安装即可,NodeJS自带 Npm 包管理工具,下载地址:https://nodejs.org/en/download/安装完成后在命令行执行以下命令查看npm包管理器版本 npm -v npm中文文档:https://www.npmjs.cn/ 配置淘宝镜像 npm install cnpm -g --registry=https://registry.npm.taobao.org 然后执行 cnpm -v 查看版本信

Vue中利用$emit实现子组件向父组件通信

Vue中利用$emit实现子组件向父组件通信 父组件 <template> <div> <p>我是父组件</p> <child :isShow="show" @hidechild="hidechild"></child> <button @click="show=true">显示子组件</button> </div> </templa

vue基础二,组件

vue二: 指令: 1,v-once指令: v-once:单独使用,限制的标签内容一旦赋值,便不可被动更改(如果是输入框,可以主动修改) <div id="app"> <input type="text" v-model="msg"> <!-- 一旦赋值,只可主动更改 --> <input type="text" v-model="msg" v-once> &

Vue子组件向父组件通信,父组件向子组件通信

首先,子组件代码如下 <template> <div style="border:1px solid black;width:400px;"> <h3>我是子组件里的</h3> <button>点击按钮子组件传递父组件</button> <div>我是父组件传子组件显示的:我还没有值</div> </div> </template> <script> ex