一、指令
< body>
< div id = " box" >
< div v-hello = " ' yellow' " > 11111111</ div>
< div v-hello = " ' red' " > 22222222</ div>
< div v-hello = " whichColor" > 33333333</ div>
</ div>
< script type = " text/javascript" >
Vue. directive ( "hello" , {
inserted ( el, binding ) {
console. log ( "inserted" , binding)
el. style. background= binding. value
} ,
update ( el, binding ) {
console. log ( "update" )
el. style. background= binding. value
}
} )
var vm = new Vue ( {
el : "#box" ,
data : {
whichColor : "blue"
}
} )
</ script>
</ body>
二、指令函数简写
< body>
< div id = " box" >
< div v-hello = " ' yellow' " > 11111111</ div>
< div v-hello = " ' red' " > 22222222</ div>
< div v-hello = " whichColor" > 33333333</ div>
</ div>
< script type = " text/javascript" >
Vue. directive ( "hello" , ( el, binding ) => {
el. style. background= binding. value
} )
var vm = new Vue ( {
el : "#box" ,
data : {
whichColor : "blue"
}
} )
</ script>
</ body>
三、指令轮播
< body>
< div id = " box" >
< header> 导航-{{myname}}</ header>
< div class = " swiper-container zzy" >
< div class = " swiper-wrapper" >
< div class = " swiper-slide" v-for = " (data,index) in datalist" :key = " data" v-swiper = " {
index:index,
length:datalist.length
}" >
< img :src = " data" />
</ div>
</ div>
< div class = " swiper-pagination" > </ div>
</ div>
< footer> 底部内容</ footer>
</ div>
< script>
Vue. directive ( "swiper" , {
inserted ( el, binding ) {
let {
index,
length
} = binding. value
if ( index === length - 1 ) {
new Swiper ( ".zzy" , {
pagination : {
el : '.swiper-pagination' ,
} ,
loop : true ,
autoplay : {
delay : 2500 ,
disableOnInteraction : false ,
}
} )
}
}
} )
new Vue ( {
el : "#box" ,
data : {
datalist : [ ] ,
myname : "zzy"
} ,
mounted ( ) {
setTimeout ( ( ) => {
this . datalist = [
"https://static.maizuo.com/pc/v5/usr/movie/e856bdc65507b99800f22e47801fa781.jpg" ,
"https://static.maizuo.com/pc/v5/usr/movie/47aa5a3ad2ebff403d073288e4365694.jpg" ,
"https://static.maizuo.com/pc/v5/usr/movie/8b0755547313706883acc771bda7709d.jpg"
]
} , 2000 )
}
} )
</ script>
</ body>
四、Vue3-指令轮播
< body>
< div id = " box" >
< header> 导航-{{myname}}</ header>
< div class = " swiper-container zzy" >
< div class = " swiper-wrapper" >
< div class = " swiper-slide" v-for = " (data,index) in datalist" :key = " data" v-swiper = " {
index:index,
length:datalist.length
}" >
< img :src = " data" />
</ div>
</ div>
< div class = " swiper-pagination" > </ div>
</ div>
< footer> 底部内容</ footer>
</ div>
< script>
var obj = {
data ( ) {
return {
datalist : [ ] ,
myname : "zzy"
}
} ,
mounted ( ) {
setTimeout ( ( ) => {
this . datalist = [
"https://static.maizuo.com/pc/v5/usr/movie/e856bdc65507b99800f22e47801fa781.jpg" ,
"https://static.maizuo.com/pc/v5/usr/movie/47aa5a3ad2ebff403d073288e4365694.jpg" ,
"https://static.maizuo.com/pc/v5/usr/movie/8b0755547313706883acc771bda7709d.jpg"
]
} , 2000 )
}
}
var app = Vue. createApp ( obj)
app. directive ( "swiper" , {
mounted ( el, binding ) {
console. log ( "1111111111111111111111111111" )
let {
index,
length
} = binding. value
if ( index === length - 1 ) {
new Swiper ( ".zzy" , {
pagination : {
el : '.swiper-pagination' ,
} ,
loop : true ,
autoplay : {
delay : 2500 ,
disableOnInteraction : false ,
}
} )
}
}
} )
app. mount ( "#box" )
</ script>
</ body>
五、轮播-nextTick
< body>
< div id = " box" >
< header> 导航-{{myname}}</ header>
< div class = " swiper-container zzy" >
< div class = " swiper-wrapper" >
< div class = " swiper-slide" v-for = " data in datalist" :key = " data" >
< img :src = " data" />
</ div>
</ div>
< div class = " swiper-pagination" > </ div>
</ div>
< footer> 底部内容</ footer>
</ div>
< script>
var vm = new Vue ( {
el : "#box" ,
data : {
datalist : [ ] ,
myname : "zzy"
} ,
mounted ( ) {
setTimeout ( ( ) => {
this . datalist = [
"https://static.maizuo.com/pc/v5/usr/movie/e856bdc65507b99800f22e47801fa781.jpg" ,
"https://static.maizuo.com/pc/v5/usr/movie/47aa5a3ad2ebff403d073288e4365694.jpg" ,
"https://static.maizuo.com/pc/v5/usr/movie/8b0755547313706883acc771bda7709d.jpg"
]
this . $nextTick ( ( ) => {
console. log ( "我比updated执行的都晚,而且只执行一次" )
new Swiper ( ".zzy" , {
pagination : {
el : '.swiper-pagination' ,
} ,
loop : true ,
autoplay : {
delay : 2500 ,
disableOnInteraction : false ,
}
} )
} )
} , 2000 )
} ,
updated ( ) {
console. log ( "updated" )
}
} )
</ script>
</ body>
六、filter
< body>
< div id = " box" >
< button @click = " handleAjax" > click-ajax</ button>
< ul>
< li v-for = " item in datalist" :key = " item.id" >
< img :src = " item.img | imgFilter1 | imgFilter2" />
{{item.nm}}
</ li>
</ ul>
</ div>
< script>
Vue. filter ( "imgFilter1" , ( url ) => {
return url. replace ( 'w.h/' , '' )
} )
Vue. filter ( "imgFilter2" , ( url ) => {
return url + '@1l_1e_1c_128w_180h'
} )
new Vue ( {
el : "#box" ,
data : {
datalist : [ ]
} ,
methods : {
handleAjax ( ) {
axios. get ( "./json/maoyan.json" ) . then ( res => {
console. log ( res. data. movieList)
this . datalist = res. data. movieList
} )
}
}
} )
</ script>
</ body>
七、启动和配置
< script>
import Vue from 'vue'
import App from './App.vue'
Vue. config. productionTip = false
new Vue ( {
render : h => h ( App)
} ) . $mount ( '#app' )
var obj = {
name : 'zzy' ,
age : 100
}
console. log ( obj)
</ script>
< template>
< div class = " about" >
< h1> This is an about page</ h1>
</ div>
</ template>
< template>
< div class = " home" >
6666
< img alt = " Vue logo" src = " ../assets/logo.png" >
< HelloWorld msg = " Welcome to Your Vue.js App" />
</ div>
</ template>
< script>
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name : 'Home' ,
components : {
HelloWorld
}
}
</ script>
< template>
< div>
hello app1111
</ div>
</ template>
八、单文件组件
< template>
< div>
hello appp-{{myname}}
< input type = " text" v-model = " mytext" />
< button @click = " handleAdd" > add</ button>
< ul>
< li v-for = " data in datalist" :key = " data" >
{{data}}
</ li>
</ ul>
< navbar myname = " home" :myright = " false" @event = " handleEvent" >
< div> 22222222</ div>
</ navbar>
< sidebar v-show = " isShow" > </ sidebar>
< div v-hello > 111111111111111111</ div>
< div v-hello > 222222222222222222</ div>
< img :src = " imgUrl | imgFilter" />
</ div>
</ template>
< script>
import navbar from '@/mycomponents/Navbar'
import sidebar from '@/mycomponents/Sidebar'
import Vue from 'vue'
import axios from 'axios'
Vue. directive ( 'hello' , {
inserted ( el, binding ) {
el. style. border = '1px solid black'
}
} )
Vue. filter ( 'imgFilter' , ( path ) => {
return path. replace ( '/w.h' , '' ) + '@1l_1e_1c_128w_180h'
} )
export default {
data ( ) {
return {
myname : 'zzy' ,
mytext : '' ,
datalist : [ ] ,
isShow : true ,
imgUrl : 'http://p0.meituan.net/w.h/movie/056d4f1a658e3e35c9f9e6c44c2aa3fe1831930.jpg'
}
} ,
components : {
navbar,
sidebar
} ,
methods : {
handleAdd ( ) {
console. log ( this . mytext)
this . datalist. push ( this . mytext)
} ,
handleEvent ( ) {
this . isShow = ! this . isShow
}
} ,
computed : {
} ,
watch : {
} ,
mounted ( ) {
axios. get ( '/zzy/ajax/movieOnInfoList?token=&optimus_uuid=74B5F0A032A711EB82DD6B9282E93C676D27D7B9731D4E608D7612C3E708C120&optimus_risk_level=71&optimus_code=10' ) . then ( res => {
console. log ( res. data)
} )
}
}
</ script>
< style lang = " scss" >
$width : 300px;
ul {
li {
background : yellow;
width : $width;
}
}
</ style>
九、一级路由和重定向
< template>
< div>
< ul>
< li>
< a href = " /#/films" > 电影</ a>
</ li>
< li>
< a href = " /#/cinemas" > 影院</ a>
</ li>
< li>
< a href = " /#/center" > 我的</ a>
</ li>
</ ul>
< router-view> </ router-view>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
}
}
}
</ script>
< style lang = " scss" >
</ style>
< template>
< div>
center
</ div>
</ template>
< template>
< div>
cinemas
</ div>
</ template>
< template>
< div>
films
</ div>
</ template>
十、嵌套路由和动态路由
< template>
< div>
< ul>
< router-link to = " /films" custom v-slot = " {navigate,isActive}" >
< li @click = " navigate" :class = " isActive?'kerwinactive':''" > 电影</ li>
</ router-link>
< router-link to = " /cinemas" custom v-slot = " {navigate,isActive}" >
< li @click = " navigate" :class = " isActive?'kerwinactive':''" > 影院</ li>
</ router-link>
< router-link to = " /center" custom v-slot = " {navigate,isActive}" >
< li @click = " navigate" :class = " isActive?'kerwinactive':''" > 我的</ li>
</ router-link>
</ ul>
< router-view> </ router-view>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
}
}
}
</ script>
< style lang = " scss" >
.kerwinactive {
color : red;
}
</ style>
十一、路由模式和路由守卫
< template>
< div>
center
</ div>
</ template>
< script>
export default {
beforeRouteEnter ( to, from, next ) {
if ( localStorage. getItem ( 'token' ) ) {
next ( )
} else {
next ( {
path : '/login' ,
query : { redirect : to. fullPath }
} )
}
}
}
</ script>
< template>
< div>
detail
</ div>
</ template>
< script>
export default {
created ( ) {
console. log ( 'created' , this . $route. params. id)
}
}
</ script>
< template>
< div>
< div style = " height : 200px; background : yellow; " > 大轮播</ div>
< div>
二级的声明式导航
</ div>
< router-view> </ router-view>
</ div>
</ template>
< template>
< div>
login
< button @click = " handleLogin" > 登录</ button>
</ div>
</ template>
< script>
export default {
methods : {
handleLogin ( ) {
setTimeout ( ( ) => {
localStorage. setItem ( 'token' , '后端返回的token字段' )
console. log ( this . $route. query)
this . $router. push ( this . $route. query. redirect)
} , 0 )
}
}
}
</ script>
< template>
< div>
< ul>
< router-link to = " /films" custom v-slot = " {navigate,isActive}" >
< li @click = " navigate" :class = " isActive?'zzyactive':''" > 电影</ li>
</ router-link>
< router-link to = " /cinemas" custom v-slot = " {navigate,isActive}" >
< li @click = " navigate" :class = " isActive?'zzyactive':''" > 影院</ li>
</ router-link>
< router-link to = " /center" custom v-slot = " {navigate,isActive}" >
< li @click = " navigate" :class = " isActive?'zzyactive':''" > 我的</ li>
</ router-link>
</ ul>
< router-view> </ router-view>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
}
}
}
</ script>
< style lang = " scss" >
.zzyactive {
color : red;
}
</ style>