新建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 = 打包上线文件
  • 继续加组件、路由、接口都可以
相关推荐
xiao5kou4chang6kai43 天前
GIS数据制备,空间分析与高级建模
arcgis·gis·空间数据
杨超越luckly3 天前
Agent应用指南:利用POST请求获取麦当劳门店位置信息
人工智能·arcgis·html·数据可视化·麦当劳
非科班Java出身GISer4 天前
ArcGIS JS 基础教程(18):SceneLayer 场景图层
arcgis·arcgis js 白膜·arcgis js 建筑·scenelayer 图层·arcgis js i3s
Li.map5 天前
CesiumJS 地图主题系统:用着色器打造实时滤镜
javascript·arcgis·cesium·着色器
中科GIS地理信息培训11 天前
【实操】在 ArcGIS Pro 中,计算多边形的面积时,需排除缓冲区要素中与多边形重叠的部分
arcgis·arcgis pro
中科GIS地理信息培训11 天前
ArcGIS Pro镶嵌数据集:绝不仅是数据之和
arcgis·arcgis pro
开心些13 天前
ArcGIS批量出图
arcgis
非科班Java出身GISer16 天前
ArcGIS JS API 5.0 ESM 双模块系统冲突解决方案
arcgis·arcgis js 引入问题·arcgis js amd·arcgis esm引入问题·arcgis js 资源冲突
中科GIS地理信息培训1 个月前
【ArcGIS Pro 3.7新增功能4】增强空间统计中【评估点聚合的图格大小】工具:分析字段和时间间隔
人工智能·算法·arcgis
雪的季节1 个月前
GIS 矢量数据格式
arcgis