对于以下问题的任何帮助将不胜感激!
情况:
我的项目包含两个包:
子组件库
- 包含以组合 API 样式编写的单个视图
About.vue
(带有 vue2 辅助库@vue/composition-api
和vuex-composition-helpers
) - 导出单个
RouteConfig
- 构建为一个库
views/About.vue
(孩子)<template> <div class="about"> <h1>This is an about page (as component lib)</h1> </div> </template> <script> import { defineComponent } from "@vue/composition-api"; import { createNamespacedHelpers } from "vuex-composition-helpers"; export default defineComponent({ components: {}, setup(_, { root }) { const { useGetters, useActions } = createNamespacedHelpers("account"); // error thrown here! } }); </script>
router/index.ts
(孩子)export const routes: Array<RouteConfig> = [{ path: "/", name: "About", component: () => import(/* webpackChunkName: "about" */ "../views/About.vue") }];
lib.ts
(孩子)export const routes = require("@/router").routes;
package.json
(孩子)"scripts": { "build": "vue-cli-service build --target lib --name child-component-lib src/lib.ts" ...
- 包含以组合 API 样式编写的单个视图
父应用
- 将路由从
child-component-lib
导入到其路由器 - 包含一个简单的视图,显示一行文本和一个
<router-view />
package.json
(父母)"dependencies": { "@tholst/child-component-lib": "file:../child-component-lib",
router/index.ts
(父母)import { routes as childComponentRoutes } from "@tholst/child-component-lib"; const routes: Array<RouteConfig> = [...childComponentRoutes]; const router = new VueRouter({routes}); export default router;
App.vue
(父母)<template> <div id="app"> <Home /> <router-view /> </div> </template> <script> import { defineComponent } from "@vue/composition-api"; import Home from "@/views/Home.vue"; export default defineComponent({ components: { Home }, setup(_, { root }) { ... } }); </script>
- 将路由从
预期行为
它可以正常工作。
实际行为
我在控制台中看到错误输出。[Vue warn]: Error in data(): "Error: You must use this function within the "setup()" method, or insert the store as first argument."
错误消息具有误导性,因为错误实际上是在setup()
方法内部引发的。它可以追溯到getCurrentInstance()
返回undefined
(内部@vue/composition-api
)。
调查:
- 事实证明,当我在父应用程序本身中包含相同内容时,错误消失
About.vue
了(只需切换路线,尝试一下),即,当我们避免从构建的库中导入时它可以工作。 - 所以看起来这是构建设置的问题(
之一,,,,,
vue.config.js
...webpack
)babel
typescript
重现错误:
1. 克隆、安装、运行
git clone git@github.com:tholst/vue-composition-api-comp-lib.git && cd vue-composition-api-comp-lib/child-component-lib && npm install && npm run build && cd ../parent-app/ && npm install && npm run serve
或一一
git clone git@github.com:tholst/vue-composition-api-comp-lib.git
cd vue-composition-api-comp-lib/child-component-lib
npm install
npm run build
cd ../parent-app/
npm install
npm run serve
2.打开浏览器
- 转到 http://localhost:8080/
3. 打开开发工具查看错误
[Vue warn]: Error in data(): "Error: You must use this function within the "setup()" method, or insert the store as first argument."
found in
---> <Anonymous>
<App> at src/App.vue
<Root>
错误截图
环境信息:
Node: 14.2.0
npm: 6.14.8
Chrome: 86.0.4240.198
npmPackages:
@vue/babel-sugar-composition-api-inject-h: 1.2.1
@vue/babel-sugar-composition-api-render-instance: 1.2.4
...
@vue/cli-overlay: 4.5.8
@vue/cli-plugin-babel: 4.5.8
@vue/cli-plugin-router: 4.5.8
@vue/cli-plugin-typescript: 4.5.8
@vue/cli-plugin-vuex:4.5.8
@vue/cli-service: 4.5.8
@vue/cli-shared-utils: 4.5.8
@vue/component-compiler-utils: 3.2.0
@vue/composition-api: 1.0.0-beta.19
@vue/preload-webpack-plugin: 1.1.2
typescript: 3.9.7
vue: 2.6.12
vue-loader: 15.9.5 (16.0.0-rc.1)
vue-router: 3.4.9
vue-template-compiler: 2.6.12
vue-template-es2015-compiler: 1.9.1
vuex: 3.5.1
vuex-composition-helpers: 1.0.21
npmGlobalPackages:
@vue/cli: 4.5.8