我的组件中有这个函数,它应该向快速 API 发送 POST 请求:
onSubmit (evt) {
evt.preventDefault();
//alert(JSON.stringify(this.description));
axios.post('http://localhost:3000/api/v1/addComment', {
articleid: this.id,
description: this.description
});
}Run Code Online (Sandbox Code Playgroud)
这是请求被发送到的 API:
router.post('/api/v1/addComment/', function(req, res){
var newComment = req.body;
//newComment.id = parseInt(commentsData.length);
commentsData.comments.push(newComment);
fs.writeFile('app/data/comments.json', JSON.stringify(commentsData), 'utf8', function(err){
console.log(err);
});
res.setHeader("Access-Control-Allow-Origin", "*");
res.json(newComment);
});Run Code Online (Sandbox Code Playgroud)
我还需要在我的快递 app.js 文件中使用必要的 CORS 依赖项
var express = require('express');
var reload = require('reload');
var app = express();
var cors = require('cors');
var dataFile = require('./data/articles.json');
app.set('port', process.env.PORT || 3000 );
//app.set('appData', dataFile);
app.set('view engine', 'ejs');
app.set('views', 'app/views');
app.use(express.static('app/public'));
app.use(require('./routes/index')); …Run Code Online (Sandbox Code Playgroud)我只是想store.subscribe()在Vuex. 我不断收到以下错误,这似乎表明没有 store.subscribe 方法,这与文档中的内容相反:
未捕获的类型错误:WEBPACK_IMPORTED_MODULE_2__store .a.state.subscribe 不是函数
这是我的代码:
app.js 文件,我在其中初始化所有内容,注册我的组件等:
import Vuex from 'vuex';
import router from './routes';
import store from './store';
window.Vue = require('vue');
const app = new Vue({
el: '#app',
router,
store: new Vuex.Store(store)
});
store.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})Run Code Online (Sandbox Code Playgroud)
我的store.js文件,作为我的集中Vuex存储。到目前为止,它一直在完美运行:
import router from './routes';
export default {
state: {
sample: {
}
},
mutations: {
sample(state){
}
},
getters: {
sample(state){
return state.sample
} …Run Code Online (Sandbox Code Playgroud)