Vue.js + iView + i18n + Axios set up

1 篇文章 0 订阅
1 篇文章 0 订阅

Vue.js + iView + i18n + Axios set up

Vue.js

Download node.js from https://nodejs.org/en/:

node_download_web

Node.js integrates NPM and you can view the version using the following command:

$ node -v
v10.13.0

$ npm -v
6.4.1

Install vue-cli:

$ npm install -g vue-cli

Open the execution command in the project directory to create the project:

$ vue init webpack <projectName>

vue_init

Test run:

$ cd lanye_project_name
$ npm run dev

run

web

If something goes wrong in the process, you can’t download node_modules, you can install again:

$ npm install

This command will find lanye_project_name/package.json - dependencies and install these to node_modules package.

+ iView

Install iView on your project:

$ cnpm install --save iview

install_iview

If it works, lanye_project_name/package.json - dependencies will add iview:

  "dependencies": {
    "iview": "^3.2.1",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  }

And lanye_project_name/node_modules will have iview package:

iview_package

Import iView globally: lanye_project_name/src/main.js:

import iView from 'iview'
import 'iview/dist/styles/iview.css'

Vue.use(iView)

Complete code:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import 'iview/dist/styles/iview.css'

Vue.config.productionTip = false
Vue.use(iView)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

And then, you can use iView on the whole project. IView docs: https://www.iviewui.com/docs/guide/install-en

+ i18n

Install iView on your project:

$ cnpm install --save i18n

install_i18n

If it works, lanye_project_name/package.json - dependencies will add vue![-](https://img-blog.csdnimg.cn/2018122911333951.png)i18n:

  "dependencies": {
    "iview": "^3.2.1",
    "vue-i18n": "^8.6.0",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  }

And lanye_project_name/node_modules will have vue-i18n package:

i18n_package

Import iView globally:

  1. lanye_project_name/src/i18n:

Create en_US.json:

{
  "message": {
    "password": "password"
  },
  "placeholder": {
  	"tips": "tips"
  }
}

Create zh_CN.json:

{
  "message": {
    "password": "密码"
  },
  "placeholder": {
  	"tips": "提示"
  }
}

Create index.js:

import VueI18n from 'vue-i18n'
import Vue from 'vue'

Vue.use(VueI18n)

var Messages = {
  zh: require('./zh_CN.json'),
  en: require('./en_US.json')
}

var i18n = new VueI18n({
  locale: 'zh',
  messages: Messages
})

export default i18n
  1. lanye_project_name/src/main.js
import i18n from './i18n'

/* add i18n */
new Vue({
  el: '#app',
  router,
  i18n,
  components: { App },
  template: '<App/>'
})

Complete code:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import i18n from './i18n'

Vue.config.productionTip = false
Vue.use(iView)
Vue.prototype.$axios = axios

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  i18n,
  components: { App },
  template: '<App/>'
})
  1. Simple example:
<div>
	<p>{{ $t("placeholder.tips") }}</p>
</div>

Input:

<Input type="text" v-model="formInline.userName" :placeholder="$t('form.userName')">

+ Axios

Install iView on your project:

$ cnpm install --save axios

install axios

If it works, lanye_project_name/package.json - dependencies will add axios:

  "dependencies": {
    "axios": "^0.18.0",
    "iview": "^3.2.1",
    "vue": "^2.5.2",
    "vue-i18n": "^8.6.0",
    "vue-router": "^3.0.1"
  }

And lanye_project_name/node_modules will have axios package:

axios_package

Import iView globally: lanye_project_name/src/main.js:

import axios from 'axios'

Vue.prototype.$axios = axios

Complete code:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import axios from 'axios'
import i18n from './i18n'

Vue.config.productionTip = false
Vue.use(iView)
Vue.prototype.$axios = axios

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  i18n,
  components: { App },
  template: '<App/>'
})

Simple usage:

//Send a `GET` request (default)
axios('/user/12345');

//Send a `POST`request
axios({
    method:"POST",
    url:'/user/12345',
    data:{
        firstName:"Fred",
        lastName:"Flintstone"
    }
});

Recommend an address that uses Axios in detail: https://www.jianshu.com/p/df464b26ae58.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值