目录
绑定事件的时候,@xxx="yyy" yyy可以写一些简单的语句
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
-
- head>
- <body>
- <div id="root">
- <h2>今天天气很{{info}}h2>
- <button @click="changeWeather">切换天气button>
-
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- const vm=new Vue({
- el:'#root',
- data:{
- isHot:true
- },
- computed:{
- info(){
- return this.isHot?'炎热':'凉爽'
- }
- },
- methods:{
- changeWeather(){
- this.isHot=!this.isHot
- }
- }
-
- })
-
- script>
- html>
监视的两种写法:
1)new Vue时传入watch配置
2)通过vm.$watch监视
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
-
- head>
- <body>
- <div id="root">
- <h2>今天天气很{{info}}h2>
- <button @click="changeWeather">切换天气button>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- const vm=new Vue({
- el:'#root',
- data:{
- isHot:true
- },
- computed:{
- info(){
- return this.isHot?'炎热':'凉爽'
- }
- },
- methods:{
- changeWeather(){
- this.isHot=!this.isHot
- }
- },
- // watch:{
- // isHot:{
- // immediate:true,
- // handler(newValue,oldValue){
- // console.log('isHot被修改了',newValue,oldValue);
- // }
- // }
- // }
- })
- vm.$watch('info',{
- immediate:true,
- handler(newValue,oldValue){
- console.log('isHot被修改了',newValue,oldValue);
- }
- })
- script>
- html>

备注:
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
-
- head>
- <body>
- <div id="root">
- <h2>今天天气很{{info}}h2>
- <button @click="changeWeather">切换天气button>
- <hr>
- <h3>a的值是:{{numbers.a}}h3>
- <button @click="numbers.a++">点我让a+1button>
- <h3>b的值是:{{numbers.b}}h3>
- <button @click="numbers.b++">点我让b+1button>
- <button @click="numbers={a:666,b:888}">彻底换掉numbersbutton>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- const vm=new Vue({
- el:'#root',
- data:{
- isHot:true,
- numbers:{
- a:1,
- b:1
- }
- },
-
- computed:{
- info(){
- return this.isHot?'炎热':'凉爽'
- }
- },
- methods:{
- changeWeather(){
- this.isHot=!this.isHot
- }
- },
- watch:{
- isHot:{
- immediate:true,
- handler(newValue,oldValue){
- console.log('isHot被修改了',newValue,oldValue);
- }
-
- },
- // 监视多级结构中某个属性的变化
- // 'numbers.a':{
- // handler(){
- // console.log('a被改变了');
- // }
- // },
- // 监视多级结构中所有属性的变化
- numbers:{
- deep:true,
- handler(){
- console.log('number改变了');
- }
- }
- }
- })
-
- script>
- html>
需要没有immediate和deep属性
简写有两种方式,一在watch,二在全局中
- isHot(newValue,oldValue){
- console.log('isHot被修改了',newValue,oldValue);
- }
- vm.$watch('isHot',function(newValue,oldValue)){
- console.log('isHot被修改了',newValue,oldValue);
- }
两个重要的小原则:
- watch:{
- firstname(val){
- setTimeout(()=>{
- this.fullName=val+'-'+this.lastname
- })
- this.fullName=newValue+'-'+this.lastname
- },
- lastname(newValue){
- this.fullName=this.firstname+'-'+newValue
- }
- }
1.v-if
写法:
适用于:切换频率较高的场景
特点:不展示DOM的元素直接被移除
注意:v-if可以和v-else-if、v-else一起使用,但要求结构不能被”打断“
2.v-show
写法:v-show="表达式"
适用于:切换频率较高的场景
特点:不展示的DOM元素未被移除,仅仅是使用样式隐藏掉
3.备注:使用v-if的时候,元素可能无法获取到,而使用v-show一定可以获取到
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
-
- head>
- <body>
- <div id="root">
- <h2>当前的n值为:{{n}}h2>
- <button @click="n++">点我n+1button>
-
- <h2 v-show="false">欢迎来到{{name}}h2>
- <h2 v-show="1===1">欢迎来到{{name}}h2>
-
-
- <h2 v-if="false">欢迎来到{{name}}h2>
- <h2 v-if="1===1">欢迎来到{{name}}h2>
-
-
- <div v-if="n===1">Angulardiv>
- <div v-else-if="n===2">Reactdiv>
- <div v-else-if="n===3">Vuediv>
- <div v-else> 哈哈div>
-
- <div>
- <h2 v-show="n===1">你好h2>
- <h2 v-show="n===1">尚硅谷h2>
- <h2 v-show="n===1">北京h2>
- div>
-
-
- <template>
- <h2>你好h2>
- <h2>guiguh2>
- <h2>北京h2>
- template>
-
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- const vm=new Vue({
- el:'#root',
- data:{
- name:'花京院',
- n:0
- }
- })
-
- script>
- html>
v-for指令
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
- head>
- <body>
- <div id="root">
-
- <h2>人员列表h2>
- <ul>
- <li v-for="(p,index) of persons" :key="index">
-
- {{p.name}}---{{p.age}}---{{index}}--{{p.id}}
- li>
- ul>
-
-
- <h2>汽车信息(遍历对象)h2>
- <ul>
- <li v-for="(value,k) of car" :key="k">
-
- {{k}}---{{value}}
- li>
- ul>
-
-
- <h2>测试遍历字符串(用得少)h2>
- <ul>
- <li v-for="(a,b) of str">
-
-
- {{a}}---{{b}}
- li>
- ul>
-
-
-
- <h2>测试遍历指定次数(用得少)h2>
- <ul>
- <li v-for="(number,index) of 5" :key="index">
- {{index}}----{{number}}
- li>
- ul>
- div>
- body>
- <script>
- Vue.config.productionTip = false; //阻止vue在启动时生成生产提示
- new Vue({
- el: "#root",
- data: {
- // 遍历数组
- persons: [
- { id: "001", name: "张三", age: 18 },
- { id: "002", name: "李四", age: 19 },
- { id: "003", name: "王五", age: 20 },
- ],
- car: {
- name: "奥迪A8",
- price: "70万",
- color: "黑色",
- },
- str:'hello'
- },
- });
- script>
- html>