vue-fetch使用教程

 

1.安装命令“

cnpm install --save isomorphic-fetch es6-promise

2.由于ie不支持Promise,所以需要安装promise-polyfill;

cnpm install promise-polyfill --save-exact

需要在index.js中引用

复制代码
import Vue from 'vue'
import Router from 'vue-router'
import Promise from "promise-polyfill";

if(!window.Promise){
    window.Promise = Promise;
}
复制代码

3.使用

复制代码
<template>
  <div class="hello">
    <h1 @click="getUrl">{{ msg }}</h1>
    <router-link to="/test/one">testlinks</router-link>
    <router-link to="/test/two" exact>testlinks1</router-link>
    <router-link to="/user/one">testlinks2</router-link>
    <transition :name="tranName">
      <router-view></router-view>
    </transition>
    <ul>
      <li v-for='item in items'>{{item.id}}</li>
    </ul>
  </div>


</template>

<script>

require("es6-promise").polyfill();
require('isomorphic-fetch');

export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      tranName:'slide-left',
      items:[]
    }
  },
  created(){
    let _this = this;
    fetch('./src/static/data.json').then(function(res){
      return res.json();
    }).then(function(stories){
      console.log(stories)
      _this.items = stories;
    }).then(function(err){
      console.log(err)
    })
  },
  methods:{
    getUrl(){
      console.log(1)
    }
  },
  beforeRouteUpdate (to, from, next) {
    const toDepth = to.path.split('/').length
    const fromDepth = from.path.split('/').length
    this.tranName = toDepth >= fromDepth ? 'slide-right' : 'slide-left'
    next()
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s
}
.fade-enter, .fade-leave-to /* .fade-leave-active in below version 2.1.8 */ {
  opacity: 0
}
.slide-left-enter-active, .slide-right-leave-active {
  transition: all .5s cubic-bezier(.55,0,.1,1);
}
.slide-left-enter, .slide-right-leave-active {
  opacity: 0;
  -webkit-transform: translate(30px, 0);
  transform: translate(30px, 0);
}
.slide-left-leave-active, .slide-right-enter {
  opacity: 0;
  -webkit-transform: translate(-30px, 0);
  transform: translate(-30px, 0);
}
</style>
复制代码

转载于:https://www.cnblogs.com/chenzhiling/p/8042191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值