vue入门

一、vue的特点

1、模板渲染
2、模块化
3、扩展功能
路由、ajax、数据流

二、内容

vue实例 模板渲染 条件渲染
组件交互 标签属性 事件绑定 计算属性 属性监听
表单 动画 vue-cli项目搭建
vue-router vuex vue组件 vue指令 内置组件
实例方法 实例选项 实例属性

三、入门

1、 项目搭建
>	创建:
		vue init webpack project-name
	依赖:
		npm install
	运行:
		npm run dev
2、案例解析
new Vue({
	  el: '#app',//挂载位置
	  router,
	  components: { App },
	  template: '<App/>'//挂载的东西,即组件
	})

var root=new Vue({
  el: '#app',
  router,
  template: '<div>{{fruit}}</div>',
  data:{
  	fruit:'apple'//数据部分
  }
})
获取对象数据:
	root.$data
对象绑定事件:
	root.$on('emit',function(){})
3、vue生命周期
		0、初始化对象
		1、beforeCreate
		2、data\method初始化
		3、created
		4、模板在内存中编译完
		5、beforeMount
		6、内存中替换到页面
		7、Mounted
		8、beforeUpdate旧数据
		9、updated数据更新
		10、beforeDestory
		11、destroyed
4、全局注册组件(组件树)
	定义:
		Vue.compenent('my-comp',{
			template:'<p>hello</p>'
		})
	调用:
		<my-comp/>
5、局部组件
		var my-comp={
			template:'<p>hello</p>'
		}
		new Vue({
		  el: '#app',
		  components:{
		  	'my-header':my-comp
		  }
		})
	    在挂载元素app位置内调用:
			<my-header/>
6、多组件组合
		var mychild={
			template:'<p>hello child</p>'
		}
		var myparent={
			template:'<p><my-chlid/>hello parent</p>',
			compenents:{
				'my-chlid':mychild
			}
		}
7、组件赋值
		避免多个相同组件同时更新
		data() {
			return {
				name:'s'
			}
		}
8、注册插件
		Vue.use()
9、指令
		v-on:事件名=""
		v-on:click=""
		缩写:@click
10、内置组件
	<router-view/>

四、入门2

1、模板内数据显示
	* {{name}}
	* v-text
	* v-html
2、列表
	* 数组
		<p v-for="(item,index) in list">
			{{index}}
			{{item.name}}
			{{item.price}}
		</p>

		data(){
			list:[{
				name:'link',
				price:'12'
			},{
				name:'min',
				price:'23'
			}],
			obj:{
				name:'link',
				price:'12'	
			}
		}
	* 对象
		<p v-for="(value,key) in obj">
			{{key}}
			{{value}}
		</p>
	* 组件
		导入模板:
			import componentA from './component/a.vue'
		注册为组件:
			export default {
				components: {'ComponentA':componentA}
			}
		渲染:	
			<ComponentA v-for="(value,key) in obj">
			</ComponentA>
3、组件内方法访问数据
	data(){
		return{
			list:[{
			   name:'link'
			}]
		}
	}
	说明:this类似java中的对象,列表更新方法pop
	methods:{
		add() {
			this.list.push({
				name:'min'
			})
		}
	}
4、v-bind绑定
	v-bind:html的标签属性
	如:	  v-bind:href="link"
	缩写:      :href="link"

	如:  :class
	     v-bind:class="str"

	如:  :style
	data(){
		return{
			link:'www.baidu.com',
			str:red-font
		}
	}
5、条件渲染
	v-if="true":dom 消失
	v-else

	v-show="true":display:none
	v-else
6、自定义事件
	<compA/ @my-event="onCompMyEvent">
	onCompMyEvent(param){
		console.log("onCompMyEvent"+param)
	}
	子组件:
	<button @click="emitMyEvent">emit</button>
	data(){
		return{
			hello:'link'
		}
	}
	emitMyEvent(){
		this.$emit('my-event',this.hello)
	}
7、表单数据绑定(双向绑定)
	<input v-model="val" type="text"></input>

	<select v-model='selection'>
		<option v-for="item in selectOptions" :value="item.value">{{item.text}}</option>
	</select>
	selection:null
	selectOptions:[
		{
		  text:'1',
		  value: '1'
		},{
		  text:'2',
		  value:'2';
		}
	]
8、v-model
	v-model.number:数字
	v-model.lazy:懒加载
	v-model.trim:去空
9、计算属性与属性监听
  * 计算属性(调用方法也可以)
	{{myVal}}

	data(){
		return{
			value:'link'
		}
	}
	在value更新时myVal也更新,即自动更新
	computed:{
		myVal(){
			return this.value
		}
	}
  * 属性监听
  	<input v-model="myVal"></input>
	data(){
		return{
			myVal:'',
			list:[
				{name:'link'}
			]
		}
	}
	在list被修改时,自动更新myVal
  	watch:{
  		myVal:function(val,oldVal){
  			console.log(val+":"+oldVal)
  		},
  		list:function(){}
  	}

五、进阶

1、父子组件通信
	parent——>child:Props
	child——>parent:Emit\Event
2、向子组件传数据
  * 例:
	父组件:
	    <child number='10'></child>
	子组件:
		引入:
			props:['number']
		调用:
			{{number}}
 *  动态传递
	<input type="text" v-model="myVal"/>
	<child v-bind:my-value="myVal"></child>

	data(){
		return{
			myVal:''
		}
	}
3、向父组件传数据
	子组件:	
	<button @click="emitMyEvent"></button>
	data(){
		return{
			name:'link'
		}
	}
	methods:{
		emitMyEvent(){
			this.$emit('myEvent',this.name)
		}
	}
	父组件:
	data(){
		return{
			msg:''
		}
	}
	<child @myEvent="getMyEvent"></child>
	getMyEvent(name){
	     this.msg=name
	}
4、插槽
	本质:父组件向子组件传(代码块)数据
	父组件:
		<child @myEvent="myevent">
	          <p>123</p>
	          <span>link min</span>
	    </child>
	子组件:
	child.vue:
		显示数据
		<slot></slot>
5、具名插槽:
  <child @myEvent="myevent">
      <p slot="header">header</p>
      <span slot="footer">footer</span>
  </child>
 child.vue:
  <template>
    <slot name="header">no heaer</slot>
    <slot name="footer">no footer</slot>
  </template>

说明:
	<slot>:显示数据插槽
	slot="":传入的数据
	slot-scope:prop外部数据
案例:	
组件与插槽分离:
<base-layout>
  <template slot="header">
    <h1>Here might be a page title</h1>
  </template>
	  ||
  <h1 slot="header">Here might be a page title</h1>
</base-layout>

base-layout.vue:
<template>
   <slot name="header">no heaer</slot>
   <slot name="footer">no footer</slot>
</template>

组件与插槽共用:
<template slot="default" slot-scope="slotProps">
   {{ slotProps.msg }}
</template>
6、组件切换
	<p :is="currentView"></p>

	import com-A from './components/com-A'
	import com-B from './components/com-B'
	components:{
		com-A,
		com-B
	},
	data(){
	  ruturn{
	  	currentView:'com-A'
	  }
	},
	methods:{
		change(){
			if(this.currentView==com-A){
				this.currentView==com-B
			}else{
				this.currentView==com-A
			}
		}
	}

六、高级

1、过渡:
  *	css过渡四个阶段:
		fade-enter
		fade-enter-active
		fade-leave-active
		fade-leave
	例:
	  <transition name="fade">
        <p v-show="res">Hello,I am world</p>
      </transition>
      .fade-enter-active,.fade-leave-active{
	      transition: all .2s;
	  }
	  .fade-enter,.fade-leave-active{
	    opacity: 0;
	   }
	用法:包含组件动态效果进入显示

  *js过渡
2、自定义指令
	<p v-color="'red'">1</p>

	directives:{
	      color:function(el,binding){
	        el.style.color=binding.value
	      }
	}
3、插件
	安装:
		npm install vue-router --save
	导入:
		import Router from 'vue-router'
		||
		var Router =require('vue-router')
	注册:
		Vue.use(Router)
	放入根组件:
4、单文件组件
5、mixins

七、工具

1、vue-cli
	* 项目架构
	* 测试
	* 打包
	* Node(4.x)
	* Git
2、安装vue-cli
	安装:
		npm install vue-cli -g
	创建项目:
		vue init webpack project-name
	安装依赖:
		npm install
	运行:
		npm run dev
	打包:
		npm run build
3、变量声明
	let:作用在{}内
	var:全局
	const:常量
4、组件以变量形式导出\导入
	export  {com}
	import {com} from './components/com'
5、路由
	本质:访问的页面的位置,与请求url保持一致
	优点:比原来的路由不用重复加载资源
	注:将router放到根节点
	let router=new Router({
		mode:'history',
		routes:[
			{
				path:'/apple',
				component:Apple,
			}
		]
	})

	路由跳转:
		默认:<router-view/>
		链接:<router-link :to="{path:'apple'}"/>
6、路由参数
url:localhost:8080/apple/red
routes:[
	{
		path:'/apple/:color',
		component:Apple,
	}
]
* 在apple.vue页面的方法获取参数:
	methods:{
		getParam(){
			console.log(this.$route.params)
		}
	}
* 在页面直接获取参数
	{{$route.params.color}}
7、路由嵌套

```java
routes:[
	{
		path:'/apple',
		component:Apple,
		children:[
			{
				path:'/red',
				component:RedApple,
			}
		]
	}
]

apple.vue:
	<template>
		//子组件的路由位置,必须包含在父组件内
		//此处为RedApple
		//此时的url:/apple/red
		<router-view/>
	</template>

路由跳转:
	<router-link :to="{path:'apple/red'}"/>
8、router-link
 基于当前路径跳转:
		<router-link to="apple"/>
		基于根目录跳转:
			<router-link to="/apple"/>
		动态绑定:
			<router-link :to="apple"/>
			data(){
				return{
					apple:'apple'
				}
			}<router-link :to="'apple'"/>
		绑定带参数:
			<router-link :to="{path:'apple',param:{color:'red'}}"/>
		具名路由:
			<router-link :to="{name:'applePage'}"/>
			routes:[
				{
					path:'/apple',
					component:Apple,
					name:'applePage'
				}
			]
		指定标签:
			<router-link :to="'apple'" tag="li"/><router-link :to="'child'" tag="a">apple</router-link>
9、编程式导航

router.push(‘apple’)

router.push({path:‘apple’})
用处:
在路由跳转前,检查用户登录信息
router.beforeEach()
未登录时转入登录页面

10、命名视图
	
apple.vue:
	<router-view name="viewA"></router-view>
	<router-view name="viewB"></router-view>	
routes:[
		{
			path:'/apple',
			components:{
				viewA:Apple,
				viewB:RedApple
			},
			name:'applePage'
		}
]
11、重定向
routes:[
	{
		path:'/',
		redirect:'/apple'
	},
	{
		path:'/apple'
		component:Apple
	}
]
12、vuex状态管理

多组件的状态交互、数据共享:
即不同的组件使用到相同的数据
Store:数据中心
更新时同步到所有组件

单向流动:
Actions:动作
|
Mutation:事件
|
State:状态
|
Components:组件更新

13、vuex使用
		安装:
			npm install vuex --save
			save为保存依赖
		引入:
			import Vuex from 'vuex'
			Vue.use(vuex)
		调用:
			let store=new Vuex.Store({
				state:{
					totalPrice:0
				},
				mutations:{
					increment(state,price){
						state.totalPrice+=price
					},
					decrement(state,price){
						state.totalPrice-=price
					}
				}
			})
			放入Vue根节点,注册为全局组件:
				new Vue({
					store
				})
			在其他组件调用:
				数据:
					this.$store.state.totalPrice
				操作:
					this.$store.commit('increment',this.price)	
					注:commit(方法名,参数)
				父组件自动刷新:
					 {{totalPrice}}
					 computed:{
					    totalPrice(){
					        return  this.$store.state.totalPrice
					    }
					  }
		action与getters:
			let store=new Vuex.Store({
				state:{
					totalPrice:0
				},
				getters:{
					getTotal(state){
						return state.totalPrice
					}
				},
				mutations:{
					increment(state,price){
						state.totalPrice+=price
					},
					decrement(state,price){
						state.totalPrice-=price
					}
				},
				actions:{
					//只能调用mutations
					increment(context,price){
						context.commit('increment',price)
					}
				}
			})
			action使用:
				this.$store.dispatch('increment',this.price)
			getters使用,避免从state获取数据:
				this.$store.getters.getTotal
		注:action为异步操作,mutation同步
			模板中定义元素必须要加在根节点下

element中prop与slot-scope区别 :

	* prop直接显示数据值
		<el-table-column label="姓名" width="180" prop="name">
		</el-table-column>
	* slot-scope可以对显示数据编辑
		<el-table-column label="姓名" width="180">
		  <template slot-scope="scope">
		    {{'a' + scope.row.name + 'b'}}
		  </template>
		</el-table-column>
Vue入门可以从以下几个方面开始: - **引入Vue**:从 Vue 的官方网站下载相关的脚本文件,或者通过类似 npm 或 Yarn 的工具安装。下载的文件包含了 Vue 的核心代码和几个常用的插件,可以通过 script 标签或操作性的方法引入到项目中。例如下载脚本后,在 HTML 文件中使用`<script src="./vue.js"></script>`引包;也可以不需要下载直接引用 Vue 库,如`<script src="https://cdn.jsdelivr.net/npm/vue"></script>` [^3]。 - **创建Vue实例**:创建一个简单的 Vue 实例来控制页面中的 DOM 区域。例如以下代码创建了一个计数器示例,点击按钮可以让`count`值自增: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <!-- vue 实例要控制的 DOM 区域 --> <div id="app"> <h3>count 的值为:{{count}}</h3> <!-- 点击按钮,让 data 中的 count 值自增 +1 --> <button @click="count+=1">+1</button> </div> <!-- 导入 vue 脚本文件 --> <script src="./lib/vue-2.6.12.js"></script> <script> // 创建 VM 实例对象 const vm = new Vue({ // 指定当前 VM 要控制的区域 el: '#app', // 数据源 data: { // 计数器的值 count: 0, }, methods: { // 点击按钮,让 count 自增 +1 // addCount() { // this.count += 1 // }, }, }) </script> </body> </html> ``` 在这个例子中,`el`指定了 Vue 实例要控制的 DOM 区域,`data`是数据源,用于存储数据 [^2]。 - **组件使用**:组件是 Vue 应用中的重要部分,有全局组件和局部组件。以全局组件为例,注册之后可以用在任何新创建的 Vue 根实例(`new Vue`)的模板中。示例代码如下: ```html <div id="app"> <runoob></runoob> </div> <script> // 定义和注册组件 Vue.component('runoob', { template: '<h1>自定义组件!</h1>' }) // 创建根实例 new Vue({ el: '#app' }) </script> ``` 这里通过`Vue.component`方法定义并注册了一个全局组件`runoob`,在根实例的模板中就可以使用该组件 [^1]。 - **生命周期钩子**:Vue 实例有自己的生命周期,在不同阶段会触发不同的钩子函数。例如: ```javascript export default { data() { return { message: 'Hello Vue!' }; }, beforeCreate() { console.log('beforeCreate'); }, created() { console.log('created'); }, beforeMount() { console.log('beforeMount'); }, mounted() { console.log('mounted'); }, beforeUpdate() { console.log('beforeUpdate'); }, updated() { console.log('updated'); }, beforeDestroy() { console.log('beforeDestroy'); }, destroyed() { console.log('destroyed'); } } ``` 这些钩子函数可以在不同的阶段执行相应的操作,帮助开发者更好地控制 Vue 应用的流程 [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值