在vue里面使用eval_vue 远程加载sfc组件思路__Vue.js

本文介绍了在Vue项目中如何通过远程加载SFC组件以解决多业务线共同开发的问题。首先,服务端使用vue-template-compiler解析.vue文件,然后客户端通过fetch获取组件配置并渲染。文章详细阐述了服务端解析组件和客户端渲染的过程,包括样式处理和JavaScript执行。

问题

在我们的 vue 项目中(特别是后台系统),总会出现一些需要多业务线共同开发同一个项目的场景,如果各业务团队向项目中提供一些公共业务组件,但是这些组件并不能和项目一起打包,因为项目中不能因为某个私有模块的频繁变更而重复构建发布。

^_^不建议在生产环境使用,代码包含eval

思路

在这种场景下我们需要将公共的业务组件部署到服务端,由客户端请求并渲染组件。

服务端解析.vue文件

使用vue-template-compiler 模板解析器,解析SFC(单文件组件)

const compile = require('vue-template-compiler')

// 获取sfc组件的源码

const str = fs.readFileSync(path.resolve(__dirname, `../components/sfc.vue`), 'utf-8')

// vue-loader内置,现在用来解析SFC(单文件组件)

let sfc = compile.parseComponent(str)

// 获取sfc组件配置

let sfcOptions = getComponentOption(sfc)getComponentOption 获取sfc组件配置

import { uuid } from 'utilscore'

import stylus from 'stylus'

import sass from 'sass'

import less from 'less'

const getComponentOption = sfc => {

// 生成data-u-id

const componentId = uuid(8, 16).toLocaleLowerCase()

// 标签添加data-u-id属性

const template = sfc.template ? tagToUuid(sfc.template.content, componentId) : ''

// 转化style(less、sass、stylus)

let styles = []

sfc.styles.forEach(sty => {

switch (sty.lang) {

case 'stylus':

stylus.render(sty.content, (err, css) =&

组件套用导致的错误:permission.js:32 [Vue warn]: Unknown custom element: <ContactsDialog> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <CustomerAdd> at src/components/Dialog_Sales_Addedit/CustomerAdd.vue <ContactsDialog> at src/components/Dialogs_Sales_Choose/CustomerDialog.vue <ContactAdd> at src/components/Dialog_Sales_Addedit/ContactAdd.vue <ContactsDialog> at src/components/Dialogs_Sales_Choose/ContactsDialog.vue <AuthorizationManagement> at src/views/DepartmentSales/serviceCenter/authorizationManagement.vue <AppMain> at src/layout/components/AppMain.vue <Layout> at src/layout/index.vue <App> at src/App.vue <Root> warn @ vue.runtime.esm.js:620 createElm @ vue.runtime.esm.js:5922 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 createChildren @ vue.runtime.esm.js:6037 createElm @ vue.runtime.esm.js:5938 patch @ vue.runtime.esm.js:6461 Vue._update @ vue.runtime.esm.js:3933 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 Watcher @ vue.runtime.esm.js:4456 mountComponent @ vue.runtime.esm.js:4061 Vue.$mount @ vue.runtime.esm.js:8399 init @ vue.runtime.esm.js:3115 createComponent @ vue.runtime.esm.js:5962 createElm @ vue.runtime.esm.js:5909 updateChildren @ vue.runtime.esm.js:6200 patchVnode @ vue.runtime.esm.js:6303 patch @ vue.runtime.esm.js:6466 Vue._update @ vue.runtime.esm.js:3936 updateComponent @ vue.runtime.esm.js:4054 get @ vue.runtime.esm.js:4467 run @ vue.runtime.esm.js:4542 flushSchedulerQueue @ vue.runtime.esm.js:4298 eval @ vue.runtime.esm.js:1979 flushCallbacks @ vue.runtime.esm.js:1905 Promise.then timerFunc @ vue.runtime.esm.js:1932 nextTick @ vue.runtime.esm.js:1989 queueWatcher @ vue.runtime.esm.js:4390 update @ vue.runtime.esm.js:4532 notify @ vue.runtime.esm.js:731 reactiveSetter @ vue.runtime.esm.js:1056 eval @ vue-router.esm.js:2504 eval @ vue-router.esm.js:2503 updateRoute @ vue-router.esm.js:1990 eval @ vue-router.esm.js:1868 eval @ vue-router.esm.js:1977 step @ vue-router.esm.js:1707 step @ vue-router.esm.js:1714 step @ vue-router.esm.js:1714 runQueue @ vue-router.esm.js:1718 eval @ vue-router.esm.js:1972 step @ vue-router.esm.js:1707 eval @ vue-router.esm.js:1711 eval @ vue-router.esm.js:1957 eval @ vue-router.esm.js:1750 eval @ vue-router.esm.js:1826 Promise.then eval @ vue-router.esm.js:1773 eval @ vue-router.esm.js:1794 eval @ vue-router.esm.js:1794 flatMapComponents @ vue-router.esm.js:1793 eval @ vue-router.esm.js:1729 iterator @ vue-router.esm.js:1936 step @ vue-router.esm.js:1710 step @ vue-router.esm.js:1714 step @ vue-router.esm.js:1714 eval @ vue-router.esm.js:1711 eval @ vue-router.esm.js:1957 eval @ permission.js:32 eval @ regenerator.js:70 eval @ regeneratorDefine.js:13 asyncGeneratorStep @ asyncToGenerator.js:8 _next @ asyncToGenerator.js:22 eval @ asyncToGenerator.js:27 eval @ asyncToGenerator.js:19 eval @ permission.js:69 iterator @ vue-router.esm.js:1936 step @ vue-router.esm.js:1710 runQueue @ vue-router.esm.js:1718 confirmTransition @ vue-router.esm.js:1965 transitionTo @ vue-router.esm.js:1867 replace @ vue-router.esm.js:2280 eval @ vue-router.esm.js:1951 eval @ permission.js:47 eval @ regenerator.js:70 eval @ regeneratorDefine.js:13 asyncGeneratorStep @ asyncToGenerator.js:8 _next @ asyncToGenerator.js:22 Promise.then asyncGeneratorStep @ asyncToGenerator.js:13 _next @ asyncToGenerator.js:22 Promise.then asyncGeneratorStep @ asyncToGenerator.js:13 _next @ asyncToGenerator.js:22 eval @ asyncToGenerator.js:27 eval @ asyncToGenerator.js:19 eval @ permission.js:69 iterator @ vue-router.esm.js:1936 step @ vue-router.esm.js:1710 runQueue @ vue-router.esm.js:1718 confirmTransition @ vue-router.esm.js:1965 transitionTo @ vue-router.esm.js:1867 init @ vue-router.esm.js:2495 beforeCreate @ vue-router.esm.js:540 invokeWithErrorHandling @ vue.runtime.esm.js:1853 callHook @ vue.runtime.esm.js:4207 Vue._init @ vue.runtime.esm.js:4988 Vue @ vue.runtime.esm.js:5069 eval @ main.js:56 ./src/main.js @ app.js:7398 __webpack_require__ @ app.js:849 fn @ app.js:151 1 @ app.js:9266 __webpack_require__ @ app.js:849 checkDeferredModules @ app.js:46 (anonymous) @ app.js:925 (anonymous) @ app.js:928 permission.js:32 [Vue warn]: Unknown custom element: <ContactsDialog> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <CustomerAdd> at src/components/Dialog_Sales_Addedit/CustomerAdd.vue <ContactsDialog> at src/components/Dialogs_Sales_Choose/CustomerDialog.vue <AuthorizationManagement> at src/views/DepartmentSales/serviceCenter/authorizationManagement.vue <AppMain> at src/layout/components/AppMain.vue <Layout> at src/layout/index.vue <App> at src/App.vue <Root>
最新发布
09-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值