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

2
src/commom/fade/FadeAnimation.vue

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

4
src/commom/gallery/Gallery.vue

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

11
src/main.js

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

5
src/pages/city/City.vue

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

12
src/pages/home/home.vue

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

5
src/router/index.js

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

8
src/store/index.js

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

3
src/store/mutations.js

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

3
src/store/state.js

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