我已经创建了一个vue组件js文件,该文件已加载到我的页面中,以执行简单的操作:
function CategoryProductViewModel() {
var props = {
id: Number,
attributes: [Array, Object],
categories: Array,
...
}
var data = function(){
return {
quantity: 1
}
};
var computed = {};
...
var methods = {};
...
return {
props: props,
computed: computed,
methods: methods,
template: "#category-product"
}
}
Vue.component('category-product', new CategoryProductViewModel());
然后我的页面中还有一些匹配的html标记:
<script type="text/x-template" id="category-product">
<li>bunch of html here that's irrelevant</li>
</script>
这一切都很好。我有六个这样构建的组件可以完美地协同工作。
我可以/如何将它们转换为.vue文件,然后理想地将它们编译为要包含在我的页面中的单个.js文件,而不是几个html模板和几个.js文件?
我知道在npm中可能会需要vue-cli,但是对于我一生来说,我无法弄清楚如何将我编写的内容转换为src / dist文件夹结构中的.vue文件。
如果有帮助,我将它们用作在PHP上运行的较大网站中的一种独立的前端应用程序。
答案 0 :(得分:1)
首先创建您的vue组件。
CategoryList.vue
<template>
The HTML you have but make sure it has one parent node.
</template>
<script>
export default {
props: {
id: Number,
attributes: [Array, Object],
categories: Array,
},
data(){
return {
quantity: 1
};
},
computed: {
},
methods: {
},
}
</script>
现在要构建它,您将可以使用webpack之类的东西,
这是我的设置
package.json(您可以删除很多这样的东西。我在移动设备上打字很麻烦)
{
"private": true,
"scripts": {
"build": "webpack",
"watch": "webpack --watch --progress",
"test": "mocha-webpack --require ./resources/assets/js/tests/setup.js ./resources/assets/js/tests/**/*.spec.js"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.3.1",
"autoprefixer": "^9.1.5",
"axios": "^0.18",
"normalize.css": "^8.0.0",
"postcss-loader": "^3.0.0",
"vue": "^2.5.17",
"vuex": "^3.0.1"
},
"devDependencies": {
"@babel/core": "^7.0.1",
"@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@vue/test-utils": "^1.0.0-beta.25",
"babel-eslint": "^9.0.0",
"babel-loader": "^8.0.2",
"browser-sync": "^2.24.7",
"browser-sync-webpack-plugin": "^2.2.2",
"copy-webpack-plugin": "^4.5.2",
"css-loader": "^1.0.0",
"eslint": "^5.6.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-loader": "^2.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-vue": "^4.7.1",
"expect": "^23.6.0",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"file-loader": "^2.0.0",
"ignore-styles": "^5.0.1",
"jest": "^23.6.0",
"jest-serializer-vue": "^2.0.2",
"jsdom": "^12.0.0",
"jsdom-global": "^3.0.2",
"mini-css-extract-plugin": "^0.4.2",
"mocha": "^5.2.0",
"mocha-webpack": "^2.0.0-beta.0",
"node-sass": "^4.9.3",
"sass-loader": "^7.1.0",
"svg-inline-loader": "^0.8.0",
"url-loader": "^1.1.1",
"vue-jest": "^2.6.0",
"vue-loader": "^15.4.2",
"vue-style-loader": "^4.1.2",
"vue-template-compiler": "^2.5.17",
"webpack": "^4.19.0",
"webpack-cli": "^3.1.0"
}
}
webpack.config.js
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
function resolve(dir) {
return path.join(__dirname, './', dir);
}
module.exports = {
mode: process.env.NODE_ENV,
entry: ['./resources/assets/entry.js'],
output: {
path: resolve('./public/'),
filename: 'js/app.js',
},
module: {
rules: [
{
test: /\.(png|jp(e*)g)$/,
loader: 'url-loader'
},
{
test: /\.(svg)$/,
loader: 'svg-inline-loader'
},
{
test: /\.(css|sass|scss)$/,
use: ['vue-style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.vue$/,
use: ['vue-loader'],
},
],
},
plugins: [
new VueLoaderPlugin(),
new MiniCssExtractPlugin({ filename: 'css/app.css' }),
new CopyWebpackPlugin([
{
from: resolve('resources/assets/images'),
to: resolve('public/images'),
toType: 'dir',
},
{
from: resolve('resources/assets/icons'),
to: resolve('public/icons'),
toType: 'dir',
},
]),
],
};
resources / assets / entry.js
require('@babel/polyfill');
require('./js/app.js');
require('./sass/app.scss');
在您的应用程序中就可以了
import Vue from 'vue';
import CategoryList from './CategoryList.vue';
Vue.component('CategoryList', CategoryList);
const APP = new Vue({
el: '#app',
});
现在,您可以在HTML页面上进行<category-list></category-list>
那是构建之后
例如 public / index.HTML
<HTML>
<head></head>
<body>
<main id="app">
<category-list></category-list>
</main>
<script src="js/app.js" type="text/javascript"></script>
</body>
</HTML>