// import Recommend from "pages/Recommend.vue"; // import Singer from "pages/Singer.vue"; // import Detail from "pages/Detail.vue"; // 改成路由懶加載方式引入:引入組件的方式從同步變爲異步 const Recommend=()=>import("pages/Recommend.vue"); const Singer=()=>import("pages/Singer.vue"); const Detail=()=>import("pages/Detail.vue");
created() { console.log("歌手建立") }, mounted() { console.log("歌手掛載") }, activated() { console.log("歌手緩存激活,進入keep-alive") }, deactivated() { console.log("歌手緩存停用,離開keep-alive") },
<!-- 逗號分隔字符串 --> <keep-alive include="a,b"> <component :is="view"></component> </keep-alive> <!-- 正則表達式 (使用 `v-bind`) --> <keep-alive :include="/a|b/"> <component :is="view"></component> </keep-alive> <!-- 數組 (使用 `v-bind`) --> <keep-alive :include="['a', 'b']"> <component :is="view"></component> </keep-alive>
<keep-alive> <router-view></router-view> </keep-alive>
export default{ set(key,data,expiresTime){ let obj={ data:data, ctime:(new Date()).getTime(),//時間戳,同Date.now() expiresTime:expiresTime||1000*60*60 // 若是沒有傳過時時間,則設置過時時間一個小時 } localStorage.setItem(key,JSON.stringify(obj)); }, get(key){ let obj=JSON.parse(localStorage.getItem(key)); let getItem=(new Date()).getTime(); if(getItem-obj.ctime>=obj.expiresTime){ // 若是超時 localStorage.removeItem(key); return null; }else{ // 未超時 return obj.data; } } }
import LocalStorage from "utils/localStorage.js";
Vue.prototype.$localStorage=LocalStorage;
this.$localStorage.set("name","吳小明") console.log(this.$localStorage.get("name"))
npm install vue-lazyload
import VueLazyLoad from "vue-lazyload"; Vue.use(VueLazyLoad,{ preLoad:1.3, // 預加載 error:require("./assets/musicLogo.png"), // 錯誤時顯示 loading:require("./assets/musicLogo.png"), // 加載時顯示 attempt:1 // 每次加載多少張 });
<img :src="info.avatar">
<img v-lazy="info.avatar">
npm install mint-ui
import MintUI from "mint-ui"; import "mint-ui/lib/style.css"; Vue.use(MintUI);
import {Toast,Indicator} from "mint-ui";
<button @click="toast">toast</button> methods: { toast(){ Toast({ message:"點擊我了,操做成功", // 信息 position:"center", // 位置 duration:3000 // 持續時間 }); } }
created() { Indicator.open({ text:"加載中", spinnerType:"fading-circle" }); setTimeout(() => { Indicator.close(); }, 2000); }
<mt-button type="default">default</mt-button>
<mt-button type="primary">primary</mt-button>
<mt-button type="danger">danger</mt-button>
<template> <div class="loading"> <img :src="img"> </div> </template> <script> // 以base64的方式引入圖片 import img from "../assets/loading.gif"; export default { data() { return { img:img } }, } </script> <style lang="less"> @import "~style/index.less"; .loading{ position: fixed; top: 0;bottom: 0; left: 0;right: 0; margin: auto; display: flex; justify-content: center; align-items: center; img{ .w(30); .h(30); } } </style>
import Loading from "components/Loading.vue"; data() { return { loading:true, songList:[] } }
<Loading v-if="loading"></Loading> <ul v-else class="songList"> <!-- --> <li v-for="(item,index) in songList" :key="index" @click="goDateil(item.creator.encrypt_uin)"> <img :src="item.imgurl"> <div class="info"> <h2>{{item.creator.name}}</h2> <p>{{item.dissname}}</p> </div> </li> </ul>
axios.get(url,(err,data)=>{ }).then((data)=>{ this.songList=data.data.list; this.loading=false; });
npm install axios qs
import axios from "./utils/axios.js";
Vue.prototype.$axios=axios;
module.exports={ devServer:{ port:8989, proxy:{ "/maoEr":{ target:"https://www.missevan.com",// 目標服務器路徑 changeOrigin:true,// 是否容許改變源,默認爲true pathRewrite:{// 重寫路徑 "^/maoEr":"" } }, } } }
mounted() { let url="/maoEr/site/getcomment"; let data={ order:1, pagesize:10, type:1, eId:1284079, p:1 } const options={ method:"POST", headers:{"content-type":"application/x-www-form-urlencoded"}, data:qs.stringify(data),// 須要下載qs依賴,npm i qs,node中queryString()有一樣效果 url:url, } this.$axios(options).then((data)=>{ console.log(data) }); }
const querystring = require('querystring');
data:qs.stringify(data),
data:querystring.stringify(data),
router.beforeEach((to,from,next)=>{ console.log("去哪裏",to) console.log("從哪來",from) let token=false; if(to.name=="About"){ if(token){ next(); }else{ next("/login"); } }else{ next(); } })
{ path: '/login', name: 'Login', component: () => import('../views/Login.vue'), beforeEnter: (to, from, next) => { console.log("去哪兒",to) console.log("從哪來",from) next(); } }
beforeRouteEnter(to, from, next) { console.log("組件進入前") next(); }
beforeRouterUpdate(to, from, next) { console.log("組件更新",to.params.id) next(); }
watch: { $route(newValue){ console.log("watch",newValue.params.id) } }
beforeRouteLeave(to, from, next) { console.log("組件要離開",this) // console.log("去哪兒", to); // console.log("從哪來", from); let state = confirm("肯定要離開嗎"); if (state) { next(); } }
npm install mint-ui
import MintUI from "mint-ui" import "mint-ui/lib/style.css" Vue.use(MintUI)
<template> <div class="home"> 這裏是首頁 <mt-loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore" > <ul> <li v-for="(item,index) in list" :key="index">{{item.name}}</li> </ul> </mt-loadmore> </div> </template> <script> export default { data() { return { list: [ { name: "AAA" }, { name: "BBB" }, { name: "CCC" }, { name: "DDD" }, { name: "EEE" }, { name: "FFF" }, { name: "RRR" }, { name: "HHH" }, { name: "NNN" } ], allLoaded: false }; }, methods: { loadTop() { let url = "/api/city/city/getHotCityList?version=6.0.4&referer=2"; this.$axios .get(url, () => {}) .then(data => { this.list = data.data.hot_city_List; // 拿到數據後讓頁面回到原位 this.$refs.loadmore.onTopLoaded(); }); }, loadBottom() { setTimeout(() => { this.list = this.list.concat([ { name: "窗前明月光" }, { name: "疑是地上霜" }, { name: "舉頭望明月" }, { name: "低頭思故鄉" } ]); console.log(this.list); this.allLoaded = false; this.$refs.loadmore.onBottomLoaded(); }, 1000); } } }; </script> <style> .home { overflow: scroll; // 注意:若是上拉函數沒法觸發,是由於這裏沒有設置超出滾動 } li { height: 50px; font-size: 20px; } .mint-loadmore-text { font-size: 20px !important; } .mint-loadmore-content { font-size: 20px !important; } </style>