一個 Yii + vue 專案(2)
阿新 • • 發佈:2018-12-26
ok,寫前端程式碼,前端要用vue,同樣的自己搭建環境,我的目錄是這樣的:
貼一下我的 package.json
{
"name": "demo",
"author": "[email protected]",
"dependencies": {
"vue": "^2.4.2"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-preset-es2015": "^6.24.1",
"css-loader ": "^0.28.4",
"file-loader": "^0.11.2",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"vue-html-loader": "^1.2.4",
"vue-loader": "^12.2.2",
"vue-router": "^2.7.0",
"vue-style-loader": "^3.0.1",
"vue-template-compiler": "^2.4.2",
"jquery": "^3.2.1",
"webpack": "^3.3.0"
}
}
還有 webpack.config.js
/* 引入操作路徑模組和webpack */
var path = require('path');
var webpack = require('webpack');
module.exports = {
/* 輸入檔案 */
entry: './src/main.js',
output: {
/* 輸出目錄,沒有則新建 */
path: path.resolve(__dirname, './dist'),
/* 靜態目錄,可以直接從這裡取檔案 */
publicPath : 'dist/',
/* 檔名 */
filename: 'build.js'
},
module: {
rules: [
/* 用來解析vue字尾的檔案 */
{
test: /\.vue$/,
loader: 'vue-loader'
},
/* 用babel來解析js檔案並把es6的語法轉換成瀏覽器認識的語法 */
{
test: /\.js$/,
loader: 'babel-loader',
/* 排除模組安裝目錄的檔案 */
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif)$/,
loader: 'url-loader?limit=8192&name=img/[name].[md5:hash:hex:7].[ext]'
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.(woff|svg|eot|ttf)\??.*$/,
loader: 'url-loader?limit=8192&name=fonts/[name].[md5:hash:hex:7].[ext]'
}
]
},
resolve: {
alias: { 'vue': 'vue/dist/vue.js' }
}
}
我把前端路由寫在 router/ 中,src 放資原始檔,src/assets/ 放靜態資源, src/components/ 放元件。
貼一下 src/main.js
import Vue from 'vue'
import App from './app.vue'
import router from '../router/default'
new Vue({
el: '#app',
router,
render: h=>h(App)
})
router/default.js
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
routes: [
]
})
src/app.vue
<template>
<div id="app">
Hello World
</div>
</template>
至於 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=3, minimum-scale=1, user-scalable=no">
<title></title>
</head>
<body>
<div id="app">
</div>
<script src="dist/build.js"></script>
</body>
</html>
ok,打包完後開啟index.html看到