我创建了一个名为 urlMixin.js 的 mixins 文件,如下所示,
module.exports = {
data() {
return {
url: 'http://localhost:3000'
};
}
}
我有一个名为 profile.vue 的组件,我在其中导入了 mixin 文件。 profile.vue 文件看起来像这样,
import axios from 'axios';
import urlMixin from './../../mixins/urlMixin';
export default{
data() {
return{
}
},
created: function(){
},
mixins : [ urlMixin ],
methods:{
getInfo: function(){
axios.get('this.url/profile')
.then(response =>{
})
.catch(e => {
this.errors.push(e);
})
}
}
}
我希望能够用 urlMixin.js 文件中存在的 url 值替换 Profile.vue 中的 url 值,就像它在 profile.vue 文件中的显示方式一样。有没有一种方法可以实现这一目标?
最佳答案
如果您使用的是 ES6,则可以使用 Template Literals将值插入字符串:
axios.get(`${this.url}/profile`)
或者使用纯 Javascript:
axios.get(this.url + '/profile')
关于javascript - 如何使用 Vue.js mixins 替换字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45803720/