新建一个components组件
JS 模块对象
样式
在App.vue引入 注意引入的实例名开头一定要大写
语法 {{exp}} 功能:
<template>
<div>
<h3>插值语法</h3>
<span>{{msg}}</span>
<h3>插值表达式</h3>
<span>转化大写{{upper.toUpperCase()}}</span>
</div>
</template>
<script>
export default {
name: "Template",
data(){
return{
msg:"我是插值语法",
upper:"joker_dj",
}
}
}
</script>
<style scoped>
</style>
触发一个事件
减少模板中的计算逻辑 进行数据缓存 依赖固定的数据类型(响应式数据)
<template>
<div>
<label>姓:<input type="text" placeholder="姓" v-model="firstName"></label><br>
<p></p>
<label>名:<input type="text" placeholder="名" v-model="lastName"></label>
<hr>
<label>姓名:<input type="text" placeholder="姓名" v-model="fullName"></label>
</div>
</template>
<script>
export default {
name: "computedAndWacth",
data(){//数据
return{
firstName:"",//姓
lastName:""//名
}
},
computed:{//计算属性
//计算姓名
fullName:{
get(){
return this.firstName+"·"+this.lastName;
}
}
}
}
</script>
<style scoped>
</style>
双向更新姓名
<template>
<div>
<label>姓:<input type="text" placeholder="姓" v-model="firstName"></label><br>
<p></p>
<label>名:<input type="text" placeholder="名" v-model="lastName"></label>
<hr>
<!--单向-->
<label>姓名:<input type="text" placeholder="姓名" v-model="fullName"></label>
<p></p>
<!--双向-->
<label>姓名:<input type="text" placeholder="姓名" v-model="fullNameTwo"></label>
</div>
</template>
<script>
export default {
name: "computedAndWacth",
data(){//数据
return{
firstName:"",//姓
lastName:""//名
}
},
computed:{//计算属性
//计算姓名
fullName:{
get(){
return this.firstName+"·"+this.lastName;
}
},
fullNameTwo:{
get(){
console.log("调用了fullNameTwo的get方法");
return this.firstName+"·"+this.lastName;
},
set(value){
console.log("调用了fullNameTwo的set方法"+value);
//更新姓名
let names=value.split('·');
this.firstName=names[0];
this.lastName=names[1];
}
}
}
}
</script>
<style scoped>
</style>
2. 侦听器watch 作用:
使用
总结:
概念
<template>
<div>
<h3>class的使用</h3>
<span :class="OneClass">样式绑定1</span>
<p></p>
<span :class="TwoClass">样式绑定2</span>
<hr>
<!--对象绑定样式-->
<span :class="{classOne:true,classTwo:false}">对象绑定样式</span> <!--true 引用样式 false 不引用-->
<p></p>
<!--数组绑定样式-->
<span :class="[OneClass,TwoClass]">数组绑定样式</span>
<hr>
<h3>style的使用</h3>
<span :style="{backgroundColor:bgColor}">动态样式 字符串</span>
</div>
</template>
<script>
export default {
name: "classAndStyle",
data(){
return{
OneClass:"classOne", //样式1
TwoClass:"classTwo", //样式2
bgColor:"red" //动态样式
}
}
}
</script>
<style scoped>
.classOne{
font-size: 24px;
}
.classTwo{
color: white;
background-color: rebeccapurple;
}
</style>
区别
v-if v-else
v-show
为什么要绑定Key?
使用shortid做为key github地址:shortid 使用: 在终端输入 npm i shortid --save
遍历对象
排序
<template>
<div>
<h3>搜索列表</h3>
<input type="text" v-model="searchName" placeholder="请输入用户姓名">
<div>
<h3>排序</h3>
<button @click="orderByAge(0)">默认</button>
<button @click="orderByAge(1)">年龄:升序</button>
<button @click="orderByAge(2)">年龄:降序</button>
</div>
<ul>
<li v-for="(p,index) in filterPersons" :key="personKeys[index]">
{{index+1}}---姓名:{{p.name}} --性别:{{p.sex}}---年龄:{{p.age}}
</li>
</ul>
<hr>
</div>
</template>
<script>
import shortId from 'shortid' //引入shortid
export default {
name: "ListRender2",
data(){
return{
searchName:"",
orderType:0,//0默认排序 1 升序 2 降序
persons:[
{name:"张三",age:18,sex:'男'},
{name:"李四",age:19,sex:'女'},
{name:"王五",age:28,sex:'男'},
{name:"赵六",age:48,sex:'女'},
{name:"张三",age:18,sex:'男'},
{name:"李四",age:19,sex:'女'},
{name:"王五",age:28,sex:'男'},
{name:"赵六",age:48,sex:'女'},
{name:"李琦",age:8,sex:'男'}
],
personKeys:[]
}
},
methods:{
orderByAge(orderType){
this.orderType=orderType;
}
},
mounted() {
this.personKeys=this.persons.map(value => shortId.generate())
},
computed:{
filterPersons(){
//获取数据
let {searchName,persons,orderType}=this;
//取出数组中的数据
let array=[...persons];
//过滤数组
if (searchName.trim()){
array =persons.filter(p=>p.name.indexOf(searchName)!==-1)
}
if(orderType){
array.sort((p1,p2)=>{
if (orderType===2){//降序
return p2.age-p1.age
}else{//升序
return p1.age-p2.age
}
})
}
return array;
}
}
}
</script>
<style scoped>
ul{
list-style: none;
}
ul li{
padding: 5px;
}
</style>
其他指令
<template>
<div>
<!--v-text-->
<p v-text="content">v-text</p>
<!--v-html-->
<p v-html="con"></p>
<!--v-pre-->
<p v-pre>{{con}}</p>
<!--v-cloak-->
<p v-cloak>{{message}}</p>
<!--ref-->
<p ref="fish">小🐟🐟🐟🐟🐟</p>
<button @click="log()">点击</button>
</div>
</template>
<script>
export default {
name: "OtherInstauct",
data(){
return{
content:"joker",
con:"<h1>joker</h1>",
message:"2020/04/25"
}
},
methods:{
log(){
console.log(this.$refs.fish.innerHTML);
}
}
}
</script>
<style scoped>
[v-cloak]{
display: none;
}
</style>
小写转大写