1:我确实安装了expressjs。2:我使用 npm install vue --save 安装了 vue 框架 3:我使用把手作为 expressjs 的模板引擎
在 index.hbs 之后,这是代码:
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
<p>\{{title2}}</p>
<script>
new Vue({
el: "#app",
data: {
title2: "Hello World"
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
现在我想使用 vue 来执行该脚本,但是我尝试过 layout.hbs 中的东西,但不知道如何包含 vue,所以我只是从 cdnjs 添加了 vue。
我还补充了一个问题,因为 hbs 没有执行脚本渲染模板,所以在 head 中无法包含在 body 端。
现在第二件事是当我想在 users.hbs 中使用 vue 时,我必须再次创建 Vue 实例,这将在更多页面继续。
它重复代码并且非常难以管理。
所以无论如何 ?我不必每次都创建实例,也不需要在来自 cdn 的 head 标记中包含脚本?
如果我在 app.js 中添加 Vue 新实例,很抱歉忘记了这一点,这是警告:
[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
(found in <Root>)
Run Code Online (Sandbox Code Playgroud)
这是 layout.hbs
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
{{{body}}}
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 3
对于 SSR,我建议使用 NUXT.js,因为他们有一个已经安装了 Express 的集成系统。使用 axios 进行 http 请求。
对于 CSR,我建议分别构建 2 个项目,其中 1 个项目有 vuejs,另一个项目有 Nodejs。然后让他们使用 axios 进行通信,为此您可以使用 EXPRESS 或 RESTIFY。
| 归档时间: |
|
| 查看次数: |
5573 次 |
| 最近记录: |