极其基础
我所知道的在ASP.NET中入门Vue最基本的方式就是将Vue脚本包含在您的项目中。您可以使用vue.js Nuget包,它会将Vue脚本添加到您的Scripts目录中,并且只需在MVC视图中包含开发或压缩版本即可。
<script src="~/Scripts/vue.min.js"></script>
然后在您的 cshtml 视图中,只需添加一个脚本块。
<script>
new Vue({
el: "#app",
data: {
message: "Hello from Vue"
}
})
</script>
#app
是指你视图上的一个元素。如果你想使用一个组件,只需要将它添加到你的脚本块中。
<script>
Vue.component("child", {
template:"<h1>I'm a child component!</h1>"
})
new Vue({
el: "#app",
data: {
message: "Hello from Vue"
}
})
</script>
并修改您的Vue模板。
<div id="app">
{{message}}
<child></child>
</div>
如果你发现自己需要构建很多组件(很可能会这样),请将它们提取到一个 vue.components.js 文件或类似的文件中,在其中定义所有组件,并在视图中引用它,除了 vue script 外。
使用单文件组件
要使用单文件组件,您需要将单文件组件的 node.js 构建过程集成到 ASP.NET MVC 构建过程中。
安装node.js。安装完成后,请全局安装 webpack。
npm install webpack -g
在您的项目中添加一个 package.json 文件。这是我使用的内容。
{
"version": "1.0.0",
"name": "vue-example",
"private": true,
"devDependencies": {
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-preset-env": "^1.1.8",
"css-loader": "^0.26.1",
"style-loader": "^0.13.1",
"vue-loader": "^11.1.0",
"vue-template-compiler": "^2.1.10",
"webpack": "^2.2.0"
},
"dependencies": {
"vue": "^2.2.1"
}
}
通常我会在项目中创建一个名为Vue的文件夹,用于存放我的Vue脚本和.vue文件。添加一个文件作为Vue构建过程的入口点。我们可以将其命名为index.js(或任何你想要的名称)。
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
创建 App.vue 文件。
<template>
<div id="app">
{{msg}}
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
在您的项目中添加一个webpack.config.js文件。这是我使用的配置。
module.exports = {
entry: "./Vue/index.js",
output: {
path: __dirname,
filename: "./Vue/bundle.js",
},
module: {
loaders: [
{ test: /\.vue$/, loader: 'vue-loader' },
],
}
}
这个配置指定 index.js 为 webpack 的入口点,用于确定 bundle.js 文件中需要包含什么内容,最终编译并放置在 Vue 文件夹中。
为了在每次构建项目时编译 bundle.js 文件,我修改了项目文件以包括以下内容。
<Target Name="RunWebpack">
<Exec Command="npm install" />
<Exec Command="webpack" />
</Target>
<Target Name="BeforeBuild" DependsOnTargets="RunWebpack"></Target>
这将安装必要的npm包,然后运行webpack来构建bundle.js。如果您正在构建项目,则这是必需的步骤。现在您只需要在具有元素
#app
的视图中包含bundle.js文件即可。您不需要包含vue.js或vue.min.js,因为它们已经包含在bundle中。我们发现,在某些情况下,我们想要使用单个文件组件,但不想将其全部打包到一个脚本中。为了做到这一点,您主要只需要修改webpack.config.js。
const fs = require("fs");
const path = require("path");
function buildEntry() {
const reducer = (entry, file) => { entry[file.split(".").shift()] = `./Vue/${file}`; return entry; };
return fs.readdirSync(path.join(__dirname, "Vue"))
.filter(file => file.endsWith(".vue"))
.reduce(reducer, {});
}
module.exports = {
entry: buildEntry(),
output: {
path: path.join(__dirname, "Vue"),
filename: "[name].js",
library: "[name]"
},
module: {
loaders: [
{ test: /\.vue$/, loader: 'vue-loader' },
],
}
}
这个 webpack 配置将为每个单独的单文件组件构建一个脚本文件。然后,您可以在使用上述“极其基础”技术的页面上包含该脚本,并通过全局或作为 Vue 的一部分来使用组件。例如,如果我有一个 Modal.vue,我会在 ASP.NET MVC 视图中包含 Modal.js,然后通过以下方式将其公开给 Vue:
Vue.component("Modal", Modal);
或者
new Vue({
...
components:{
"Modal": Modal
}
})
Webpack非常可配置,因此您可能希望采用不同的方法为每个页面构建单个捆绑包。
最后,在开发过程中,您可以打开命令行并运行
webpack --watch
在你的项目中直接进行,每次保存时将构建你的捆绑包(bundle)或单独的组件。