YuJian
/
Storyofvue
Archived
1
0
Fork 0
Browse Source

Commit

dependabot/npm_and_yarn/acorn-6.4.1
遇见 5 years ago
parent
commit
c6b13cdd53
  1. 4
      src/App.vue
  2. 2
      src/commom/fade/FadeAnimation.vue
  3. 4
      src/commom/gallery/Gallery.vue
  4. 11
      src/main.js
  5. 5
      src/pages/city/City.vue
  6. 21
      src/pages/city/components/Alphabet.vue
  7. 6
      src/pages/city/components/list.vue
  8. 6
      src/pages/city/components/search.vue
  9. 5
      src/pages/detail/components/banner.vue
  10. 4
      src/pages/detail/components/header.vue
  11. 2
      src/pages/detail/components/list.vue
  12. 3
      src/pages/home/components/Header.vue
  13. 5
      src/pages/home/components/Icons.vue
  14. 1
      src/pages/home/components/Swiper.vue
  15. 12
      src/pages/home/home.vue
  16. 5
      src/router/index.js
  17. 8
      src/store/index.js
  18. 3
      src/store/mutations.js
  19. 3
      src/store/state.js

4
src/App.vue

@ -1,7 +1,9 @@
<template> <template>
<div id="app"> <div id="app">
<!-- 保持TCP连接 -->
<keep-alive> <keep-alive>
<router-view /> <!-- <router-view>标签会渲染路径匹配到的视图组件例如这里的<router-view>代表的是根目录 -->
<router-view></router-view>
</keep-alive> </keep-alive>
</div> </div>
</template> </template>

2
src/commom/fade/FadeAnimation.vue

@ -1,8 +1,10 @@
<template> <template>
<!-- 提供两个插槽 -->
<transition><slot></slot></transition> <transition><slot></slot></transition>
</template> </template>
<script> <script>
// 退
export default { export default {
name: "Fade" name: "Fade"
}; };

4
src/commom/gallery/Gallery.vue

@ -24,6 +24,7 @@ export default {
}, },
data: function() { data: function() {
return { return {
// swiper
swiperOptions: { swiperOptions: {
pagination: ".swiper-pagination", pagination: ".swiper-pagination",
paginationType: "fraction", paginationType: "fraction",
@ -34,10 +35,11 @@ export default {
}, },
methods: { methods: {
handleGalleryClick: function() { handleGalleryClick: function() {
// close
this.$emit("close"); this.$emit("close");
} }
} }
} };
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>

11
src/main.js

@ -1,22 +1,29 @@
import Vue from "vue"; import Vue from "vue";
import App from "./App.vue"; import App from "./App.vue";
// 引入Vue-router组件,用于管理多页面跳转。
import router from "./router"; import router from "./router";
// 引入fastclick库,用于移除移动端点击300毫秒延迟
import fastClick from "fastclick"; import fastClick from "fastclick";
// 引入VueAwesomeSwiper库,用于实现首页轮播图效果
import VueAwesomeSwiper from "vue-awesome-swiper"; import VueAwesomeSwiper from "vue-awesome-swiper";
// 引入Vuex创建的仓库
import store from "./store"; import store from "./store";
import "babel-polyfill"; import "babel-polyfill";
// CSS文件引入 // 引入全局CSS文件
import "styles/reset.css"; import "styles/reset.css";
import "styles/border.css"; import "styles/border.css";
import "styles/iconfont.css"; import "styles/iconfont.css";
import "swiper/dist/css/swiper.css"; import "swiper/dist/css/swiper.css";
Vue.config.productionTip = false; Vue.config.productionTip = false;
// 在全局中引入fastclick库,移除移动端点击300毫秒延迟。
fastClick.attach(document.body); fastClick.attach(document.body);
Vue.use(VueAwesomeSwiper) // VueAwesomeSwiper是标准的Vue插件,含有install方法,所以使用Vue.use()调用更方便。
Vue.use(VueAwesomeSwiper);
new Vue({ new Vue({
router, router,
// 创建根Vue实例时把由Vuex创建的仓库传递进去,每一个组件都可以使用store。
store, store,
render: h => h(App) render: h => h(App)
}).$mount("#app"); }).$mount("#app");

5
src/pages/city/City.vue

@ -11,6 +11,7 @@
</template> </template>
<script> <script>
//
import CityHeader from "./components/header"; import CityHeader from "./components/header";
import CitySearch from "./components/search"; import CitySearch from "./components/search";
import CityList from "./components/list"; import CityList from "./components/list";
@ -33,6 +34,7 @@ export default {
}; };
}, },
methods: { methods: {
// AJAX
getCityInfo: function() { getCityInfo: function() {
axios.get("/api/city.json").then(this.getCityInfoSucc); axios.get("/api/city.json").then(this.getCityInfoSucc);
}, },
@ -44,7 +46,10 @@ export default {
this.hotCities = data.hotCities; this.hotCities = data.hotCities;
} }
}, },
// ChangehandleLetterChange
handleLetterChange: function(Letter) { handleLetterChange: function(Letter) {
// Alphabetletter
// Listwatch
this.letter = Letter; this.letter = Letter;
} }
}, },

21
src/pages/city/components/Alphabet.vue

@ -28,10 +28,12 @@ export default {
timer: null timer: null
}; };
}, },
updata: function() { updated: function() {
// refA
this.startY = this.$refs["A"][0].offsetTop; this.startY = this.$refs["A"][0].offsetTop;
}, },
computed: { computed: {
// cities
letters: function() { letters: function() {
const letters = []; const letters = [];
for (let i in this.cities) { for (let i in this.cities) {
@ -41,37 +43,46 @@ export default {
} }
}, },
methods: { methods: {
// touchstarttouchmovetouchend
handleLetterClick: function(e) { handleLetterClick: function(e) {
// Change
this.$emit("Change", e.target.innerText); this.$emit("Change", e.target.innerText);
}, },
handleTouchStart: function() { handleTouchStart: function() {
// touchstart.prevent
// touchstart
this.touchStatus = true; this.touchStatus = true;
}, },
handleTouchMove: function(e) { handleTouchMove: function(e) {
// touchstarttouchStatustrue
if (this.touchStatus) { if (this.touchStatus) {
if (this.timer) { if (this.timer) {
clearTimeout(this.timer); clearTimeout(this.timer);
} }
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
// clientY
// 79header
const touchY = e.touches[0].clientY - 79; const touchY = e.touches[0].clientY - 79;
// 20
const index = Math.floor((touchY - this.startY) / 20); const index = Math.floor((touchY - this.startY) / 20);
if (index >= 0 && index < this.letters.length){ if (index >= 0 && index < this.letters.length) {
// Change
this.$emit("Change", this.letters[index]); this.$emit("Change", this.letters[index]);
} }
}, 16); }, 16);
} }
}, },
handleTouchEnd: function() { handleTouchEnd: function() {
//
this.touchStatus = false; this.touchStatus = false;
} }
} }
} };
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>
@import '~styles/variable.styl' @import '~styles/variable.styl'
// CSS touch-action: none; touch // CSS touch-action: none; touch
// https://blog.csdn.net/lijingshan34/article/details/88350456
// * // *
// touch-action: none // touch-action: none
.list .list

6
src/pages/city/components/list.vue

@ -52,6 +52,7 @@ export default {
}, },
computed: { computed: {
...mapState({ ...mapState({
// citycurrentCity
currentCity: "city" currentCity: "city"
}) })
}, },
@ -60,18 +61,23 @@ export default {
// mutationsactions // mutationsactions
// this.$store.dispatch("changeCity", city); // this.$store.dispatch("changeCity", city);
this.$store.commit("changeCityMutations", city); this.$store.commit("changeCityMutations", city);
// <router-link>
this.$router.push("/"); this.$router.push("/");
} }
}, },
watch: { watch: {
//
letter: function() { letter: function() {
if (this.letter) { if (this.letter) {
// HTML使refDOM
const element = this.$refs[this.letter][0]; const element = this.$refs[this.letter][0];
// 使BScroll
this.BScroll.scrollToElement(element); this.BScroll.scrollToElement(element);
} }
} }
}, },
mounted: function() { mounted: function() {
// .wrapper使BScroll
this.BScroll = new BScroll(this.$refs.wrapper); this.BScroll = new BScroll(this.$refs.wrapper);
} }
}; };

6
src/pages/city/components/search.vue

@ -36,7 +36,9 @@ export default {
}, },
methods: { methods: {
handleCityClick: function(city) { handleCityClick: function(city) {
// Vuex commitmutations
this.$store.commit("changeCityMutations", city); this.$store.commit("changeCityMutations", city);
//
this.$router.push("/"); this.$router.push("/");
} }
}, },
@ -48,6 +50,7 @@ export default {
}; };
}, },
computed: { computed: {
// hasNoDatatrue
hasNoData: function() { hasNoData: function() {
return !this.list.length; return !this.list.length;
} }
@ -62,13 +65,16 @@ export default {
return; return;
} }
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
//
const result = []; const result = [];
for (let i in this.cities) { for (let i in this.cities) {
this.cities[i].forEach(value => { this.cities[i].forEach(value => {
if ( if (
// indexOf
value.spell.indexOf(this.keyword) > -1 || value.spell.indexOf(this.keyword) > -1 ||
value.name.indexOf(this.keyword) > -1 value.name.indexOf(this.keyword) > -1
) { ) {
// value
result.push(value); result.push(value);
} }
}); });

5
src/pages/detail/components/banner.vue

@ -21,6 +21,7 @@
</template> </template>
<script> <script>
//
import CommitGallery from "commom/gallery/Gallery"; import CommitGallery from "commom/gallery/Gallery";
import FadeAnimation from "commom/fade/FadeAnimation"; import FadeAnimation from "commom/fade/FadeAnimation";
@ -34,7 +35,7 @@ export default {
data: function() { data: function() {
return { return {
showGallery: false showGallery: false
} };
}, },
components: { components: {
CommitGallery: CommitGallery, CommitGallery: CommitGallery,
@ -42,9 +43,11 @@ export default {
}, },
methods: { methods: {
handleBannerClick: function() { handleBannerClick: function() {
// Gallery
this.showGallery = true; this.showGallery = true;
}, },
handleGalleryClose: function() { handleGalleryClose: function() {
// Gallery
this.showGallery = false; this.showGallery = false;
} }
} }

4
src/pages/detail/components/header.vue

@ -31,7 +31,7 @@ export default {
opacity = opacity > 1 ? 1 : opacity; opacity = opacity > 1 ? 1 : opacity;
this.opacityStyle = { this.opacityStyle = {
opacity: opacity opacity: opacity
} };
this.showAbs = false; this.showAbs = false;
} else { } else {
this.showAbs = true; this.showAbs = true;
@ -44,7 +44,7 @@ export default {
deactivated: function() { deactivated: function() {
window.removeEventListener("scroll", this.handleScroll); window.removeEventListener("scroll", this.handleScroll);
} }
} };
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>

2
src/pages/detail/components/list.vue

@ -37,7 +37,7 @@ export default {
line-height .8rem line-height .8rem
font-size .32rem font-size .32rem
padding 0 .2rem padding 0 .2rem
// item-childrenitem-titleitem-children // item-childrenitem-titleitem-children
// .item-children // .item-children
// padding 0 .2rem // padding 0 .2rem
</style> </style>

3
src/pages/home/components/Header.vue

@ -8,6 +8,7 @@
</div> </div>
<router-link to="/city"> <router-link to="/city">
<div class="header-right"> <div class="header-right">
<!-- 来自Vuex中的state -->
{{ this.city }}<span class="iconfont">&#xe65c;</span> {{ this.city }}<span class="iconfont">&#xe65c;</span>
</div> </div>
</router-link> </router-link>
@ -15,11 +16,13 @@
</template> </template>
<script> <script>
// mapStateVuexAPI
import { mapState } from "vuex"; import { mapState } from "vuex";
export default { export default {
name: "HomeHeader", name: "HomeHeader",
computed: { computed: {
// city
...mapState(["city"]) ...mapState(["city"])
} }
}; };

5
src/pages/home/components/Icons.vue

@ -1,6 +1,7 @@
<template> <template>
<div class="icons"> <div class="icons">
<swiper :options="swiperOption"> <swiper :options="swiperOption">
<!-- 根据页数v-for循环出两个swiper-slide -->
<swiper-slide v-for="(page, index) of pages" :key="index"> <swiper-slide v-for="(page, index) of pages" :key="index">
<div class="icon" v-for="item of page" :key="item.id"> <div class="icon" v-for="item of page" :key="item.id">
<div class="icon-img"> <div class="icon-img">
@ -17,14 +18,16 @@
export default { export default {
name: "HomeIcons", name: "HomeIcons",
props: { props: {
//
list: Array list: Array
}, },
data: function() { data: function() {
return { return {
swiperOption: { swiperOption: {
// swiper
autoplay: false autoplay: false
} }
} };
}, },
computed: { computed: {
// 8 // 8

1
src/pages/home/components/Swiper.vue

@ -20,6 +20,7 @@ export default {
}, },
data: function() { data: function() {
return { return {
// swiper
swiperOption: { swiperOption: {
pagination: ".swiper-pagination", pagination: ".swiper-pagination",
loop: true loop: true

12
src/pages/home/home.vue

@ -15,6 +15,7 @@ import HomeSwiper from "./components/Swiper";
import HomeIcons from "./components/Icons"; import HomeIcons from "./components/Icons";
import HomeRecommend from "./components/Recommend"; import HomeRecommend from "./components/Recommend";
import HomeWeekend from "./components/Weekend"; import HomeWeekend from "./components/Weekend";
//
import axios from "axios"; import axios from "axios";
import { mapState } from "vuex"; import { mapState } from "vuex";
@ -29,6 +30,7 @@ export default {
HomeRecommend: HomeRecommend, HomeRecommend: HomeRecommend,
HomeWeekend: HomeWeekend HomeWeekend: HomeWeekend
}, },
// data
data: function() { data: function() {
return { return {
lastCity: "", lastCity: "",
@ -43,12 +45,17 @@ export default {
}, },
methods: { methods: {
getHomeInfo: function() { getHomeInfo: function() {
// 使axiosgetajaxgetHomeInfoSucc
axios.get("/api/index.json?city=" + this.city).then(this.getHomeInfoSucc); axios.get("/api/index.json?city=" + this.city).then(this.getHomeInfoSucc);
}, },
getHomeInfoSucc: function(res) { getHomeInfoSucc: function(res) {
//
res = res.data; res = res.data;
//
if (res.ret && res.data) { if (res.ret && res.data) {
//
const data = res.data; const data = res.data;
// $data
this.swiperList = data.swiperList; this.swiperList = data.swiperList;
this.iconList = data.iconList; this.iconList = data.iconList;
this.recommendList = data.recommendList; this.recommendList = data.recommendList;
@ -57,12 +64,17 @@ export default {
} }
} }
}, },
// Vueactivated
mounted: function() { mounted: function() {
// citylastCity
this.lastCity = this.city; this.lastCity = this.city;
this.getHomeInfo(); this.getHomeInfo();
}, },
// 访使keep-alivemounted
activated: function() { activated: function() {
// citycity
if (this.lastCity !== this.city) { if (this.lastCity !== this.city) {
// lastCityajax
this.lastCity = this.city; this.lastCity = this.city;
this.getHomeInfo(); this.getHomeInfo();
} }

5
src/router/index.js

@ -6,8 +6,11 @@ Vue.use(Router);
export default new Router({ export default new Router({
routes: [ routes: [
{ {
// 路由路径
path: "/", path: "/",
// 命名路由
name: "Home", name: "Home",
// 映射组件到路由,按需加载
component: () => import("@/pages/home/home") component: () => import("@/pages/home/home")
}, },
{ {
@ -16,11 +19,13 @@ export default new Router({
component: () => import("@/pages/city/City") component: () => import("@/pages/city/City")
}, },
{ {
// 动态路由参数,以冒号标记
path: "/detail/:id", path: "/detail/:id",
name: "Detail", name: "Detail",
component: () => import("@/pages/detail/Detail") component: () => import("@/pages/detail/Detail")
} }
], ],
// 切换路由时让页面滚动到初始位置
scrollBehavior(to, from, savedPosition) { scrollBehavior(to, from, savedPosition) {
return { x: 0, y: 0 }; return { x: 0, y: 0 };
} }

8
src/store/index.js

@ -1,7 +1,9 @@
import Vue from "vue"; import Vue from "vue";
import Vuex from "vuex"; import Vuex from "vuex";
import state from "./state" // 引入外部state文件
import mutations from "./mutations" import state from "./state";
// 引入外部mutations文件
import mutations from "./mutations";
Vue.use(Vuex); Vue.use(Vuex);
@ -9,9 +11,11 @@ export default new Vuex.Store({
state: state, state: state,
// 不存在大量同步操作或者异步操作,所以组件可以直接调用mutations,而不需要actions转发 // 不存在大量同步操作或者异步操作,所以组件可以直接调用mutations,而不需要actions转发
// actions: { // actions: {
// 定义的方法接受两个参数,一个是ctx,一个是city
// changeCity: function(ctx, city) { // changeCity: function(ctx, city) {
// ctx.commit("changeCityMutations", city); // ctx.commit("changeCityMutations", city);
// } // }
// }, // },
// 调用mutations方法,这里的mutations是一个外部引入的文件
mutations: mutations mutations: mutations
}); });

3
src/store/mutations.js

@ -1,8 +1,11 @@
export default { export default {
// 这个方法需要两个参数,第一个参数指的是所有公用数据(即Vuex.Store中定义的state),第二个参数是传递进来的参数
changeCityMutations: function(state, city) { changeCityMutations: function(state, city) {
state.city = city; state.city = city;
// localStorage使用需要搭配try-catch,因为有些浏览器可能关闭了localStorage,例如隐身模式
try { try {
if (localStorage.city) { if (localStorage.city) {
// localStorage是HTML5提供的本地存储API
localStorage.city = city; localStorage.city = city;
} }
} catch (e) {} } catch (e) {}

3
src/store/state.js

@ -1,4 +1,6 @@
// 定义一个默认城市
let defaultcity = "上海"; let defaultcity = "上海";
// localStorage使用需要搭配try-catch,因为有些浏览器可能关闭了localStorage,例如隐身模式
try { try {
if (localStorage.city) { if (localStorage.city) {
defaultcity = localStorage.city; defaultcity = localStorage.city;
@ -6,5 +8,6 @@ try {
} catch (e) {} } catch (e) {}
export default { export default {
// city显示默认城市
city: defaultcity city: defaultcity
}; };