我们在网站中使用Vue2,而不是SPA,因此我们准时使用单个文件组件来改善用户体验 .
Ssr在entry_server.js中使用此代码,我们可以从命令行调用此文件,例如 node ssr-bundle.js 或从快速APP:
// entry_server.js
import Test from 'components/Test.vue';
const app = new Vue({
render: h => (Test, {
props: {}
})
});
export default function(context) {
return new Promise((resolve, reject) => {
resolve(app);
});
};
这是一个示例组件:
// Test.vue
{{apidata}}
import axios from 'axios';
export default {
data() {
return {
apidata: []
};
},
beforeCreate() {
axios.get('/api/v1/versions.json', {params: {card_id: 274}}).then(response => {
this.apidata = response;
console.log('loadData!!!');
console.log(response);
});
},
};
当我们需要从API获取数据时,组件呈现正常但它不显示此数据,渲染不等待API请求 .
我们发现了许多使用vuex和路由器的SSR示例和doc,但是, how to make SSR with a single file component prefetch data from API and without vuex or router?