我实际上是在尝试安装vuex-project并从https://www.nativescript.org/blog/data-management-with-sqlite-and-vuex-in-a-nativescript-vue-app实现代码,除了我只有/ app而不是/ src ...总之,我遇到了以下错误
TypeError: cannot assign to read only property of object '#<Object>'
在main.js中
import Vue from 'nativescript-vue'
import Vuex from 'vuex'
import App from './components/App'
import VueDevtools from 'nativescript-vue-devtools'
if(TNS_ENV !== 'production') {
Vue.use(VueDevtools)
}
// Prints Vue logs when --env.production is *NOT* set while building
Vue.config.silent = (TNS_ENV === 'production')
const Sqlite = require("nativescript-sqlite");
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
database: null,
data: []
},
mutations: {
init(state, data) {
state.database = data.database;
},
load(state, data) {
state.data = [];
for(var i = 0; i < data.data.length; i++) {
state.data.push({
firstname: data.data[i][0],
lastname: data.data[i][1]
});
}
},
save(state, data) {
state.data.push({
firstname: data.data.firstname,
lastname: data.data.lastname
});
},
},
actions: {
init(context) {
(new Sqlite("my.db")).then(db => {
db.execSQL("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)").then(id => {
context.commit("init", { database: db });
}, error => {
console.log("CREATE TABLE ERROR", error);
});
}, error => {
console.log("OPEN DB ERROR", error);
});
},
insert(context, data) {
context.state.database.execSQL("INSERT INTO people (firstname, lastname) VALUES (?, ?)", [data.firstname, data.lastname]).then(id => {
context.commit("save", { data: data });
}, error => {
console.log("INSERT ERROR", error);
});
},
query(context) {
context.state.database.all("SELECT firstname, lastname FROM people", []).then(result => {
context.commit("load", { data: result });
}, error => {
console.log("SELECT ERROR", error);
});
}
}
});
Vue.prototype.$store = store;
module.exports = store;
所以在最后一行中,我使用了导出...我缺少什么?
答案 0 :(得分:0)
在module.exports = store;
文件中声明所有内容后,您实际上并不需要main.js
,至少它不会为您的应用程序/代码添加任何值。
此问题似乎是webpack的未解决问题,请查看Github问题以获取更多详细信息。