新建vue

1. 先把项目文件夹改名(必须做)

把你那个叫 vue2项目 的文件夹,改成:vue2-project

然后打开终端,进入这个文件夹

bash

运行

复制代码
cd vue2-project

2. 初始化项目(生成 package.json)

bash

运行

复制代码
npm init -y

现在不会报错了。

3. 安装 Vue2 和开发工具

bash

运行

复制代码
npm install vue@2
npm install webpack webpack-cli webpack-dev-server vue-loader vue-template-compiler css-loader babel-loader @babel/core @babel/preset-env html-webpack-plugin -D

这一步会安装所有依赖,需要等几十秒。

4. 创建 4 个核心文件

在项目里新建这 4 个文件,直接复制我给的代码:

① webpack.config.js(打包配置)

js

复制代码
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      { test: /\.vue$/, loader: 'vue-loader' },
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.css$/, use: ['vue-style-loader', 'css-loader'] }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({ template: './public/index.html' })
  ],
  devServer: { open: true, hot: true },
  mode: 'development'
};

② .babelrc(Babel 配置)

json

复制代码
{
  "presets": ["@babel/preset-env"]
}

③ public/index.html(页面模板)

html

预览

复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>Vue2 项目</title>
</head>
<body>
  <div id="app"></div>
</body>
</html>

④ src/main.js(入口文件)

js

复制代码
import Vue from 'vue'
import App from './App.vue'

new Vue({
  el: '#app',
  render: h => h(App)
})

⑤ src/App.vue(Vue 组件)

vue

复制代码
<template>
  <div id="app">
    <h1>我的第一个 Vue2 项目</h1>
    <p>成功运行啦!</p>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>
h1 {
  color: #42b983;
}
</style>

5. 配置启动命令(重要)

打开 package.json ,找到 "scripts",改成:

json

复制代码
"scripts": {
  "dev": "webpack serve",
  "build": "webpack"
},

6. 启动项目!

bash

运行

复制代码
npm run dev

浏览器会自动打开,看到绿色标题就说明成功了!


你现在能做的事

  • App.vue 里的内容 = 改页面
  • npm run build = 打包上线文件
  • 继续加组件、路由、接口都可以
相关推荐
中科GIS地理信息培训11 天前
【ArcGIS Pro 3.7新增功能4】增强空间统计中【评估点聚合的图格大小】工具:分析字段和时间间隔
人工智能·算法·arcgis
雪的季节12 天前
GIS 矢量数据格式
arcgis
非科班Java出身GISer12 天前
ArcGIS JS 基础教程(11):飞行定位 goTo
arcgis·arcgis js 飞行定位·arcgis js 定位·arcgis js 各种定位·arcgis js 飞行·arcgis js 定位到对象
我是Superman丶12 天前
前端技术手势识别
arcgis
da-peng-song15 天前
ArcGIS Desktop使用入门(四)——生成经纬度坐标
arcgis·经纬度坐标
da-peng-song15 天前
ArcGIS Desktop使用入门(三)图层右键工具——定义查询
数据库·arcgis·拆分数据·定义查询
星座52815 天前
破解水环境空间分析难题,迈向智慧水环境管理:ArcGIS水质评价、污染预测与洪水监测核心技术揭秘
arcgis·水环境·水文
非科班Java出身GISer16 天前
ArcGIS JS 基础教程(10):Camera 相机控制
arcgis·arcgis js 相机·arcgis js 相机控制·arcgis js 视角控制·arcgis js 飞行定位·arcgis js 定位·arcgis js 各种定位
码语智行17 天前
Shapefile获取空间数据和中心点坐标
java·arcgis
码语智行17 天前
地图上图、空间拓扑查询示例
java·arcgis