Home > Article > Web Front-end > How to reference vue in .js file
Methods to reference vue in js files: 1. Define a variable context to receive vue, and then execute the initContext method exported by http.js; 2. Create a main.js export vue instance, and then use it in the Just introduce the example into js.
The operating environment of this tutorial: windows7 system, vue2.0 version, thinkpad t480 computer.
Refer to the vue instance in the js file
The first method:
1. First, in http.js: define a variable context for Receive vue, then set the parameter passed in to the initVue method to be vue, and export this method.
import axios from 'axios' const TIME_OUT_MS = 60 * 1000 // 默认请求超时时间 let context = null // 定义一个变量,用来代替this(vue) function handleResults (response) { context.$router.push('/login') return result } export default { // 写一个此文件引入vue的方法,然后export导出去 initContext (vue) { context = vue }, post (url, data, response, exception) { },
2. Then in main.js: execute the initContext method exported by http.js
var vue = new Vue({ el: '#app', router, components: {App}, template: '<App/>' }) Vue.prototype.http = http //挂载http的时候执行引入vue的方法 Vue.prototype.http.initContext(vue) // 传入vue实例
Recommendation: "vue tutorial"
Two methods:
1. Main.js exports vue instance:
var vue = new Vue({ el: '#app', router, components: { App }, template: '<App/>' }) export default vue
2. Introduce
import context from '../main.js' context.$router.push('/login')
into the js that needs to be used. For more programming-related knowledge, please visit : Introduction to Programming! !
The above is the detailed content of How to reference vue in .js file. For more information, please follow other related articles on the PHP Chinese website!