欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3之 Vue CLI多環(huán)境配置

 更新時(shí)間:2021年11月15日 09:48:02   作者:久曲鍵  
這篇文章主要介紹了Vue3之 Vue CLI多環(huán)境配置,通俗點(diǎn)說就是使用配置文件來管理多環(huán)境,實(shí)現(xiàn)環(huán)境的切換,西阿棉詳細(xì)內(nèi)容,需要的朋友可以參考一下

一、前言

這里相對于之前就沒那么麻煩了,通俗點(diǎn)說就是使用配置文件來管理多環(huán)境,實(shí)現(xiàn)環(huán)境的切換。

二、實(shí)現(xiàn)切換

1、增加開發(fā)和生產(chǎn)配置文件

在web的根目錄下,創(chuàng)建開發(fā)環(huán)境切換配置文件.env.dev,內(nèi)容如下:

NODE_ENV=development
VUE_APP_SERVER=http://127.0.0.1:8880

在web的根目錄下,創(chuàng)建線上環(huán)境切換配置文件.env.prod,內(nèi)容如下:

NODE_ENV=production
VUE_APP_SERVER=https://www.baidu.com

2、修改編譯和啟動(dòng)支持多環(huán)境

package.json中修改,就是吧原來的server做下調(diào)整,

示例代碼如下:

{
  "name": "web",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve-dev": "vue-cli-service serve --mode dev --port 8080",
    "serve-prod": "vue-cli-service serve --mode prod",
    "build-dev": "vue-cli-service build --mode dev",
    "build-prod": "vue-cli-service build --mode prod",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "@ant-design/icons-vue": "^5.1.9",
    "ant-design-vue": "^2.0.0-rc.3",
    "axios": "^0.21.0",
    "vue": "^3.0.0",
    "vue-router": "^4.0.0-0",
    "vuex": "^4.0.0-0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-typescript": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-typescript": "^7.0.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0",
    "typescript": "~4.1.5"
  }
}

點(diǎn)擊右側(cè)npm中的刷新按鈕,查看效果如下:

為了看到效果,我們在main.ts添加輸出日志代碼,以便驗(yàn)證是否修改成功,

添加代碼如下:

console.log('環(huán)境',process.env.NODE_ENV);
console.log('服務(wù)端',process.env.VUE_APP_SERVER);


知識點(diǎn):

  • NODE_ENV為配置文件對應(yīng)的NODE_ENV變量
  • VUE_APP_SERVER為配置文件對應(yīng)的VUE_APP_SERVER變量

重新編譯,啟動(dòng)服務(wù),結(jié)果如下圖:

3、修改axios請求地址支持多環(huán)境

為什么要修改?

因?yàn)橐粋€(gè)系統(tǒng)不可能只有一個(gè)請求,再者每個(gè)請求都寫全路徑,這會(huì)使代碼的維護(hù)成本很大,所以,這里我們采用統(tǒng)一的配置去維護(hù)會(huì)相對好些。

因?yàn)槭侨值?,所以只要?code>main.ts中修改即可,引用axios,并設(shè)置默認(rèn)訪問路徑,

示例代碼如下:

import {createApp} from 'vue';
import Antd from 'ant-design-vue';
import App from './App.vue';
import 'ant-design-vue/dist/antd.css';
import router from './router';
import store from './store';
import axios from 'axios';
axios.defaults.baseURL=process.env.VUE_APP_SERVER;

//優(yōu)點(diǎn)就是方便開發(fā),缺點(diǎn)就是打包的時(shí)候會(huì)使文件較大(但并影響什么)
createApp(App).use(store).use(router).use(Antd).mount('#app')

console.log('環(huán)境', process.env.NODE_ENV);
console.log('服務(wù)端', process.env.VUE_APP_SERVER);


然后,我們在home修改axios的請求地址,只剩路徑即可,

示例代碼如下:

<template>
  <a-layout>
    `<a-layout-sider width="200" style="background: #fff">
      <a-menu
          mode="inline"
          v-model:selectedKeys="selectedKeys2"
          v-model:openKeys="openKeys"
          :style="{ height: '100%', borderRight: 0 }"
      >
        <a-sub-menu key="sub1">
          <template #title>
                <span>
                  <user-outlined />
                  subnav 1
                </span>
          </template>
          <a-menu-item key="1">option1</a-menu-item>
          <a-menu-item key="2">option2</a-menu-item>
          <a-menu-item key="3">option3</a-menu-item>
          <a-menu-item key="4">option4</a-menu-item>
        </a-sub-menu>
        <a-sub-menu key="sub2">
          <template #title>
                <span>
                  <laptop-outlined />
                  subnav 2
                </span>
          </template>
          <a-menu-item key="5">option5</a-menu-item>
          <a-menu-item key="6">option6</a-menu-item>
          <a-menu-item key="7">option7</a-menu-item>
          <a-menu-item key="8">option8</a-menu-item>
        </a-sub-menu>
        <a-sub-menu key="sub3">
          <template #title>
                <span>
                  <notification-outlined />
                  subnav 3
                </span>
          </template>
          <a-menu-item key="9">option9</a-menu-item>
          <a-menu-item key="10">option10</a-menu-item>
          <a-menu-item key="11">option11</a-menu-item>
          <a-menu-item key="12">option12</a-menu-item>
        </a-sub-menu>
      </a-menu>
  </a-layout-sider>
    `
    <a-list item-layout="vertical" size="large"
            :grid="{ gutter: 16, column: 3 }" :data-source="ebooks1">
      <template #renderItem="{ item }">
        <a-list-item key="item.name">
          <template #actions>
          <span v-for="{ type, text } in actions" :key="type">
            <component v-bind:is="type" style="margin-right: 8px"/>
            {{ text }}
          </span>
          </template>
          <a-list-item-meta :description="item.description">
            <template #title>
              <a :href="item.href" rel="external nofollow" >{{ item.name }}</a>
            </template>
            <template #avatar><a-avatar :src="item.cover" /></template>
          </a-list-item-meta>
        </a-list-item>
      </template>
    </a-list>
  </a-layout>
</template>

<script lang="ts">
import {defineComponent, onMounted, reactive, ref, toRef} from 'vue';
import axios from 'axios';
import {LikeOutlined, MessageOutlined, StarOutlined} from '@ant-design/icons-vue';

const listData: Record<string, string>[] = [];

export default defineComponent({
  components: {
    StarOutlined,
    LikeOutlined,
    MessageOutlined,
  },
  name: 'Home',
  setup(){
    const pagination = {
      onChange: (page: number) => {
        console.log(page);
      },
      pageSize: 3,
    };
    const actions: Record<string, string>[] = [
      { type: 'StarOutlined', text: '156' },
      { type: 'LikeOutlined', text: '156' },
      { type: 'MessageOutlined', text: '2' },
    ];
    console.log('set up');
    //使用ref進(jìn)行數(shù)據(jù)綁定
    const ebooks=ref();
    // 使用reactive進(jìn)行數(shù)據(jù)綁定
    const ebooks1=reactive({books:[]})
    onMounted(()=>{
      axios.get("/ebook/list?name=").then(response => {
        console.log("onMounted");
        const data = response.data;
        ebooks.value = data.content;
        ebooks1.books = data.content;
        console.log(response);
      })
    })
    return {
      pagination,
      actions,
      ebooks1: ebooks,
      ebooks2: toRef(ebooks1, "books")
    }

  }
});
</script>
<style scoped>
.ant-layout-sider {
  float: left;
}

.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>

我們再次重新編譯啟動(dòng),查看結(jié)果如下:

由紅圈處查看,證明修改axios請求地址,實(shí)現(xiàn)全局配置維護(hù)成功。

知識點(diǎn):

  • 多環(huán)境配置文件要放在web根目錄下
  • .env.xxx,后綴xxx和package.json里的指令的–mode xxx對應(yīng)
  • 增加–port參數(shù)來修改啟動(dòng)端口
  • 自定義變量必須以VUE_APP_開頭
  • 通過設(shè)置axios.defaults.baseURL,來統(tǒng)一設(shè)置后端的IP端口或域名

到此這篇關(guān)于Vue3之 Vue CLI多環(huán)境配置的文章就介紹到這了,更多相關(guān)Vue CLI多環(huán)境配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue使用driver.js做引導(dǎo)頁

    Vue使用driver.js做引導(dǎo)頁

    Driver.js是一個(gè)功能強(qiáng)大且高度可定制的基于原生JavaScript開發(fā)的新用戶引導(dǎo)庫,本文主要介紹了Vue使用driver.js做引導(dǎo)頁,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解

    rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解

    這篇文章主要為大家介紹了rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Vue實(shí)現(xiàn)調(diào)用PC端攝像頭實(shí)時(shí)拍照

    Vue實(shí)現(xiàn)調(diào)用PC端攝像頭實(shí)時(shí)拍照

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)調(diào)用PC端攝像頭實(shí)時(shí)拍照,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue+elementUl導(dǎo)入文件方式(判斷文件格式)

    vue+elementUl導(dǎo)入文件方式(判斷文件格式)

    這篇文章主要介紹了vue+elementUl導(dǎo)入文件方式(判斷文件格式),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue導(dǎo)入Echarts實(shí)現(xiàn)折線散點(diǎn)圖

    Vue導(dǎo)入Echarts實(shí)現(xiàn)折線散點(diǎn)圖

    這篇文章主要為大家詳細(xì)介紹了Vue導(dǎo)入Echarts實(shí)現(xiàn)折線散點(diǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • vue3.0+vant3.0快速搭建項(xiàng)目的實(shí)現(xiàn)

    vue3.0+vant3.0快速搭建項(xiàng)目的實(shí)現(xiàn)

    本文主要介紹了vue3.0+vant3.0快速搭建項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類似走馬燈效果)

    vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類似走馬燈效果)

    下面小編就為大家分享一篇vue實(shí)現(xiàn)圖片滾動(dòng)的示例代碼(類似走馬燈效果),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • vue模版編譯詳情

    vue模版編譯詳情

    本文的初衷是想讓更多哎學(xué)習(xí)的人知道并了解vue模版編譯,所以文中主要以階段流程為主,不會(huì)涉及過多的底層代碼邏輯,需要的朋友可以參考一下
    2021-09-09
  • vue-cli常用設(shè)置總結(jié)

    vue-cli常用設(shè)置總結(jié)

    本文給大家總結(jié)了vue-cli常用設(shè)置,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn)

    搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn)

    這篇文章主要介紹了搭建Vue從Vue-cli到router路由護(hù)衛(wèi)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評論