✨vue引入组件 axios和icont矢量图标

axios

在vue项目开发中,我们使用axios进行ajax请求,很多人一开始使用axios的方式,会当成vue-resoure的使用方式来用,即在主入口文件引入import VueResource from 'vue-resource'之后,直接使用Vue.use(VueResource)之后即可将该插件全局引用了,所以axios这样使用的时候就报错了,很懵逼

axios 是一个基于 promise 的 HTTP 库,axios并没有install 方法,所以是不能使用vue.use()方法的。vue插件

 

看了vue-axios的源码,它是按照vue插件的方式去写的。那么结合vue-axios,就可以去使用vue.use方法了

首先在主入口文件main.js中引用:

import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios,axios);

之后就可以使用了,在组件文件中的methods里去使用了

getNewsList(){
      this.axios.get('api/getNewsList').then((response)=>{
        this.newsList=response.data.data;
      }).catch((response)=>{
        console.log(response);
      })
}

这个是插件使用方式

icont

在你vue项目public中创建static文件,并且把刚刚下载的iconfont.js这个文件放到里面去,然后再index.html中引入

<script src="./static/iconfont.js"></script>

为了更方便使用,在components里面封装一个icon-components组件,代码如下:

//components/Icon-svg
<template>
  <svg class="svg-icon" aria-hidden="true">
    <use :xlink:href="iconName"></use>
  </svg>
</template>

<script>
export default {
  name: 'icon-svg',
  props: {
    iconClass: {
      type: String,
      required: true
    }
  },
  computed: {
    iconName () {
      return `#${this.iconClass}`
    }
  }
}
</script>

<style>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>

然后再main.js里面全局注册这个组件

import IconSvg from '@/components/icon-component'

Vue.component('icon-svg', IconSvg)

接下来就可以在你需要的地方使用了,例如,我需要使用这个icon

 <icon-svg icon-class="taobao" />

 

posted @ 2020-02-08 15:50  容忍君  阅读(511)  评论(0)    收藏  举报