Use Pinia in ts file report an error:
getActivePinia was called with no active Pinia. Did you forget to install pinia
1. Create the store.ts file
import { createPinia } from "pinia";
const store = createPinia();
export { store };
2、 Introduced in main.ts
import { createApp } from "vue";
import "./style.css";
import "./main.css";
import App from "./App.vue";
import ElementPlus from "element-plus";
import Avue from "@smallwei/avue";
import "@smallwei/avue/lib/index.css";
import "@/assets/css/mapbox-gl.css";
import { store } from "@/pinia/index"; // Introduce the created pinia
import router from "@/router/index";
import "element-plus/dist/index.css";
const app = createApp(App);
app.use(router);
app.use(store); // use pinia
app.use(ElementPlus);
app.use(Avue);
app.mount("#app");
3、 Create your own Pinia file
import { defineStore } from "pinia";
interface State {
nodeArr: any[] | [];
stencilLoading: boolean;
selectNodeId: number;
lcNodeData: LCDataType | {}
}
const defaultState: State = {
nodeArr: [], // Node data in the drag and drop bar
stencilLoading: true, // Loading of the drag bar
selectNodeId: -1, // The node id of the selected drag bar
lcNodeData: {}, // The LC data in the selected canvas
};
export const lcDataStore = defineStore("lcData", {
state: () => {
return defaultState;
},
getters: {},
actions: {},
});
4、 Using Pinia in the TS file
import { lcDataStore } from "@/pinia/lcAssign";
import { store } from "@/pinia/index";
const lcStore = lcDataStore(store); // Here you have to pass in the store
// After that, you can use the properties and methods inside the lcStore
Read More:
- [Solved] Router Pinia error: getActivePinia was called with no active Pinia. Did you forget to install pinia
- How to Solve Vscode save Vue file eslint error
- How to solve Uncaught (in promise) error in VUE?
- How to Solve Uncaught (in promise) Error (Two Solutions)
- How to Solve “Vue is not defined” Error
- Using ts-node to Execute .ts files Error [Solved]
- Golang: How to Read File All Content in one time
- [Vue warn]: Error in callback for watcher “value“ (How to Solve)
- [Solved] error TS1086: An accessor cannot be declared in an ambient context
- ERROR in [eslint] ESLint is not a constructor (How to Solve)
- [Solved] ERROR in ./src/main.ts Module not found: Error: Can‘t resolve ‘xxx‘
- Vue: How to Solve error avoided redundant navigation to current location: “/xxx”
- How to Solve Vue add element Install Error
- How to Solve Vue route jump repeated clicks Error
- How to Solve npm-install Command -4048 Error
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- How to Solve Vue3 Import lodash error
- React: How to Solve Web3 import error
- How to Solve Nodejs error: cannot find module ‘. /application’
- How to Solve electron import page Error