因此,我在VueJS中构建了一个页面应用程序,它运行得很好,但是SEO很糟糕,所以我决定制作一个普通的VueJS站点,其中有些页面有VueJS代码(远程托管,这样就不会有其他节点了)。
我跟踪了本指南,它可以工作
我有一个search.js,它包含我的VueJS实例和方法等
Vue.component('todo-component', {
template: '#todo-component',
data: function () {
return {
items: [
{
id: 'item-1',
title: 'Checkout vue',
completed: false
}, {
id: 'item-2',
title: 'Use this stuff!!',
completed: false
}
],
newItem: ''
};
},
methods: {
addItem: function () {
if (this.newItem) {
var item = {
id: Math.random(0, 10000),
title: this.newItem,
completed: false
};
this.items.push(item);
this.newItem = '';
}
}
}
});
var app = new Vue({
el: '#vue-app'
});但是,我需要导入一些东西,比如axios和其他组件。
如果我将一个导入语句添加到上面的脚本中,它将产生
import axios from "axios";未登录的SyntaxError:意外标识符
我的进口货应该去哪里?
发布于 2018-10-14 16:04:11
由于您正在直接编写在浏览器中运行的代码,所以在加载axios之前,只需在html代码中包含search.js cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>至于组件导入,您可以阅读更多关于组件注册的内容。通常,如果您的组件是通过Vue.component('my-component', {})语法全局注册的,那么您应该能够在代码中直接使用它。
发布于 2018-10-14 18:24:49
您缺少了axios库,因此将其添加如下:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>我还向您介绍了如何在使用浏览器时使用它:
new Vue({
el: '#app',
data: {
dataReceived: '',
},
methods: {
getData() {
axios.get('https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD')
.then((response) => {
this.dataReceived = response.data;
console.log(this.dataReceived);
return this.dataReceived;
})
}
}
})<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<button @click="getData" type="button">getData</button>
<p>dataReceived: {{ dataReceived }}</p>
</div>
</body>
https://stackoverflow.com/questions/52804353
复制相似问题