文章目录
问题和分析
[Vue warn]: Component provided template option but runtime compilation is not supported in this build of Vue. Configure your bundler to alias “vue” to “vue/dist/vue.esm-bundler.js”.
翻译:组件提供的模板选项,但在Vue的这个版本中不支持运行时编译。
但是,配置已经开启了热部署。
问题:vue3模板渲染模板template不支持编译
代码
import { createRouter, createWebHashHistory } from 'vue-router'
// 1. 定义路由组件.
// 也可以从其他文件导入
const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }
//路由--模版引用
// const Home = import("../components/Home.vue")
// const About = import("../components/About.vue")
// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
//路由--模版一用
// { path: '/', component: ()=> Home },
// { path: '/about', component: ()=> About }
{ path: '/', component: Home },
{ path: '/about', component: About }
]
// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置,但我们在这里
// 暂时保持简单
const router = createRouter({
// 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
history: createWebHashHistory(),
routes, // `routes: routes` 的缩写
})
export default router
解决方式
路由组件的template修改为render
const Home = { template: '<div>Home</div>' }
const About = { template: '<div>About</div>' }
// 修改为以下方式
const Home = { render(){ return 'Home'} }
const About = { render(){ return 'About'} }
修改vue项目的xxxx.config.js配置文件
vite 构建的项目vite.config.js
export default defineConfig({
plugins: [vue()],
server:{
port:"4000",
hmr:true
},
alias:{
'vue': 'vue/dist/vue.esm-bundler.js'
}
})
vue-cli构建的项目webpack.config.js
module.exports = { runtimeCompiler: true }
引用vue模版文件
上边的方式,是template中的内容模式,我们的目的是直接饮用xxxx.vue模版文件
import { createRouter, createWebHashHistory } from 'vue-router'
// 1. 定义路由组件.
// 也可以从其他文件导入
// const Home = { template: '<div>Home</div>' }
// const About = { template: '<div>About</div>' }
//路由--模版引用的文件
const Home = import("../components/Home.vue")
const About = import("../components/About.vue")
// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
//路由--模版一用
{ path: '/', component: ()=> Home },
{ path: '/about', component: ()=> About }
// { path: '/', component: Home },
// { path: '/about', component: About }
]
// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置,但我们在这里
// 暂时保持简单
const router = createRouter({
// 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
history: createWebHashHistory(),
routes, // `routes: routes` 的缩写
})
export default router