[英]How to use the same javascript file in a Vue file and a HTML file?
我有一個名為urlMixin.js的JS文件。
urlMixin.js
export function serverUrl() { return 'http://localhost:3000'; }
我有一個名為Login.vue的Vue文件。 我可以通過導入在Vue文件中使用urlMixin.js文件。
Login.vue
import {serverUrl} from "./../mixins/urlMixin.js"; let url = serverUrl(); export default { data() { return { username: '', password: '' }; }, created(){ axios.post(`${url}/login`) .then(resposne =>{ }) .catch(error =>{ }) } }
有一個html文件index.html,我要在其中使用urlMixin.js中存在的URL。
index.html
<head> <head> <body> <script src="/src/js/App.js"></script> <script src="/src/mixins/urlMixin.js"></script> <script> $(document).ready(function() { App.init(); var serverURL; }); </script> </body>
我想將相同的URL導入index.html文件,以便可以使用urlMixin.js文件返回的字符串並將其分配給變量serverURL。 我該如何實現? 有人可以幫幫我嗎?
使用axios.create函數您可以在此處提供基本URL參數,並將axios作為vue插件全局導入
我在main.js文件中將axios設置為全局。 根據文檔,可以指定配置默認值,該默認值將應用於每個請求。
import Vue from 'vue' import router from './routes' import axios from 'axios'; axios.defaults.baseURL = 'http://localhost:3000'; window.axios = axios; new Vue({ el: '#app', router })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.