【发布时间】:2021-05-04 19:30:16
【问题描述】:
我是 vuex 的新手,我无法设置它。 我的商店文件夹结构如下所示
- 商店
- 模块示例
- index.ts
- mutations.ts
- getters.ts
- state.ts
- index.ts
- store-flag.d.ts
文件如下所示: index.ts
import { store } from 'quasar/wrappers';
import Vuex from 'vuex';
import example from './module-example';
// import { ExampleStateInterface } from './module-example/state';
/*
* If not building with SSR mode, you can
* directly export the Store instantiation
*/
export interface StateInterface {
// Define your own store structure, using submodules if needed
// example: ExampleStateInterface;
// Declared as unknown to avoid linting issue. Best to strongly type as per the line above.
example: unknown;
}
export default store(function ({ Vue }) {
Vue.use(Vuex);
const Store = new Vuex.Store<StateInterface>({
modules: {
example
},
// enable strict mode (adds overhead!)
// for dev mode only
strict: !!process.env.DEBUGGING
});
if (process.env.DEV && module.hot) {
module.hot.accept(['./showcase'], () => {
const newShowcase = require('./showcase').default
Store.hotUpdate({ modules: { showcase: newShowcase } })
})
}
return Store;
});
商店标志.d.ts
/* eslint-disable */
// THIS FEATURE-FLAG FILE IS AUTOGENERATED,
// REMOVAL OR CHANGES WILL CAUSE RELATED TYPES TO STOP WORKING
import "quasar/dist/types/feature-flag";
declare module "quasar/dist/types/feature-flag" {
interface QuasarFeatureFlags {
store: true;
}
}
index.ts
import { Module } from 'vuex';
import { StateInterface } from '../index';
import state, { ExampleStateInterface } from './state';
import getters from './getters';
import mutations from './mutations';
const exampleModule: Module<ExampleStateInterface, StateInterface> = {
namespaced: true,
getters,
mutations,
state
};
export default exampleModule;
mutations.ts
import { MutationTree } from 'vuex';
import state, { ExampleStateInterface } from './state';
const mutation: MutationTree<ExampleStateInterface> = {
someMutation (state: ExampleStateInterface, token: ExampleStateInterface) {
state = token
}
};
export default mutation;
getters.ts
import { GetterTree } from 'vuex';
import { StateInterface } from '../index';
import { ExampleStateInterface } from './state';
const getters: GetterTree<ExampleStateInterface, StateInterface> = {
getToken (state: ExampleStateInterface): string {
return state.token
},
getUserName (state: ExampleStateInterface): string {
return state.username
},
getRetrievalTime (state: ExampleStateInterface): Date {
return state.retrievalTime
}
};
export default getters;
state.ts
export interface ExampleStateInterface {
token: string;
username: string;
retrievalTime: Date;
}
function state(): ExampleStateInterface {
return { token:'dddddd', username: 'ddd', retrievalTime: new Date() }
};
export default state;
我可以像这样访问状态
console.log(this.$store.state.example.retrievalTime)
但是由于任何类型为any而返回错误
但是,我无法执行突变。我试过了,但似乎没有任何反应。
this.$store.commit('example/someMutation', { token:'new', username: 'new', retrievalTime: new Date() })
我无法在线找到任何适用于 quasar typescript 的示例。任何建议都可以接受。
【问题讨论】:
标签: typescript vue.js vuex quasar-framework quasar