柳传志 潘石屹做水果网站,砀山做网站的公司,私人网站服务器免费,网站推广策划案怎么选加载远程Vue文件
vue3-sfc-loader
vue3-sfc-loader #xff0c;它是Vue3/Vue2 单文件组件加载器。
在运行时从 html/js 动态加载 .vue 文件。无需 Node.js 环境#xff0c;无需 (webpack) 构建步骤。
主要特征
支持 Vue 3 和 Vue 2#xff08;参见dist/#xff09;仅需…加载远程Vue文件
vue3-sfc-loader
vue3-sfc-loader 它是Vue3/Vue2 单文件组件加载器。
在运行时从 html/js 动态加载 .vue 文件。无需 Node.js 环境无需 (webpack) 构建步骤。
主要特征
支持 Vue 3 和 Vue 2参见dist/仅需要 Vue 仅运行时构建提供esm和umd捆绑包示例嵌入式ES6模块支持含import()TypeScript 支持、JSX 支持自定义 CSS、HTML 和脚本语言支持请参阅pug和stylus示例SFC 自定义块支持通过日志回调正确报告模板、样式或脚本错误专注于组件编译。网络、样式注入和缓存由您决定参见下面的示例轻松构建您自己的版本并自定义您需要支持的浏览器
编写Node接口
编写Node接口提供服务用于返回vue文件
项目初始化和安装
mkdir nodeServe
cd nodeServe
npm iniy -y
npm install express cors项目完整结构
nodeServer
├── index.js
├── loaderVue2.vue
├── loaderVue3.vue
├── package-lock.json
└── package.json添加 index.js
// express 基于Node.js平台快速、开放、极简的 Web 开发框架 https://www.expressjs.com.cn/
const express require(express)
const app express()
const cors require(cors)
const fs require(fs);// 配置cors中间件允许跨域
app.use(cors())app.get(/getVue2Str, (req, res) {// 服务端读取文件并变成字符串。传递给前端const data fs.readFileSync(./loaderVue2.vue, utf8);res.send({code:200,fileStr:data,fileName:loaderVue2.vue});
})app.get(/getVue3Str, (req, res) {// 服务端读取文件并变成字符串。传递给前端const data fs.readFileSync(./loaderVue3.vue, utf8);res.send({code:200,fileStr:data,fileName:loaderVue2.vue});
})app.listen(3000, () {console.log(服务启动成功http://localhost:3000)
})这里用到的两个vue文件代码如下
loaderVue2.vue
templatedivh1我是远程加载的组件/h1input :valuevalue inputchangeName /button clickpatchParentEvent触发父组件方法/button/div
/template
script
export default {props: [value],methods: {changeName(e) {this.$emit(input, e.target.value);},patchParentEvent() {this.$emit(parentEvent);},},
};
/scriptstyle scoped
h1 {color: red;
}
/styleloaderVue3.vue
templatedivh1 classtext-red我是远程加载的页面/h1input v-modelinput placeholderplaceholder inputchangeValue/button clickemitParentFun调用父组件的方法/button/div
/templatescript setup
import {defineProps,defineEmits,ref,onMounted} from vueconst props defineProps([modelValue])
// 更新model绑定的值固定写法: update:modelValue
const emit defineEmits([update:modelValue,childClick])let input ref()onMounted((){input.value props.modelValue// window环境指向的是接收方的window环境console.log(window.testName);
})const changeValue (e) {// 修改父组件的值emit(update:modelValue,e.target.value)
}const emitParentFun (){emit(childClick,input.value)
}
/scriptstyle scope
.text-red{color: red;
}
/style运行
node index.js接口返回的格式如下
http://localhost:3000/getVue2Str
{code: 200,fileStr: template\r\n div\r\n h1我是远程加载的组件/h1\r\n input :value\value\ input\changeName\ /\r\n button click\patchParentEvent\触发父组件方法/button\r\n /div\r\n/template\r\nscript\r\nexport default {\r\n props: [\value\],\r\n methods: {\r\n changeName(e) {\r\n this.$emit(\input\, e.target.value);\r\n },\r\n patchParentEvent() {\r\n this.$emit(\parentEvent\);\r\n },\r\n },\r\n};\r\n/script\r\n\r\nstyle scoped\r\nh1 {\r\n color: red;\r\n}\r\n/style\r\n,fileName: loaderVue2.vue
}Vue2项目使用
安装 vue3-sfc-loader
npm install vue3-sfc-loader使用 注意 vue2要从dist/vue2-sfc-loader这个目录下引入loadModule使用 vue2要从dist/vue3-sfc-loader这个目录下引入loadModule使用 templatedivcomponent :isremote v-bind$attrs v-ifremote v-modelname parentEventparentEvent/component/div
/templatescript
import * as Vue from vue
import {loadModule} from vue3-sfc-loader/dist/vue2-sfc-loaderexport default {name: App,data() {return {name: 李四,remote: null,url: http://localhost:3000/getVue2Str,}},mounted() {this.load(this.url)},watch: {name(newName) {console.log(newName, 监听到变化)}},methods: {// 加载async load(url) {let res await fetch(url).then(res res.json());const options {moduleCache: {vue: Vue},async getFile() {return res.fileStr},addStyle(textContent) {const style Object.assign(document.createElement(style), {textContent})const ref document.head.getElementsByTagName(style)[0] || nulldocument.head.insertBefore(style, ref)},};// 加载远程组件this.remote await loadModule(res.fileName || loader.vue, options)},// 子组件调用parentEvent() {console.log(父组件事件触发)}}
}
/script效果显示 Vue3项目使用
安装
npm install vue3-sfc-loader使用 注意 vue2要从dist/vue2-sfc-loader这个目录下引入loadModule使用 vue2要从dist/vue3-sfc-loader这个目录下引入loadModule使用 templatedivcomponent :isremote v-ifremote v-modelname childClickchildClick//div
/templatescript setup
import {loadModule} from vue3-sfc-loader/dist/vue3-sfc-loader
import * as Vue from vue
import {onMounted, defineAsyncComponent, ref, watchEffect} from vuelet remote ref()
let name ref(李四)
let url http://localhost:3000/getVue3StronMounted(() {load(url)
})watchEffect(() {console.log(name.value)
})const childClick (newVal) {console.log(子组件点击事件, newVal)
}// 加载远程文件
const load async (url) {let res await fetch(url).then(res res.json());const options {moduleCache: {vue: Vue},async getFile() {return res.fileStr},addStyle(textContent) {const style Object.assign(document.createElement(style), {textContent})const ref document.head.getElementsByTagName(style)[0] || nulldocument.head.insertBefore(style, ref)},};// 加载远程组件remote.value defineAsyncComponent(() loadModule(res.fileName || loader.vue, options))
}
/script完整源码
https://gitee.com/szxio/load-remote-vue-components 求Start 文章转载自: http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.yrck.cn.gov.cn.yrck.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn http://www.morning.tkrpt.cn.gov.cn.tkrpt.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.pccqr.cn.gov.cn.pccqr.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.wgcng.cn.gov.cn.wgcng.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.srzhm.cn.gov.cn.srzhm.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.kkrnm.cn.gov.cn.kkrnm.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn