vue 一步一步来

1. 安装项目

首先是通过: https://github.com/vuejs-templates/webpack 下载代码并新建一个项目:

$ cnpm install -g vue-cli
$ vue init webpack my-project
$ cd my-project
$ cnpm install
$ cnpm run dev

安装过程有提示你选择技术栈:

? Project name f7-vue-demo
? Project description A Vue.js project
? Author 学江 
? Vue build standalone
? Use ESLint to lint your code? Yes
? Pick an ESLint preset Standard
? Setup unit tests with Karma + Mocha? Yes
? Setup e2e tests with Nightwatch? Yes

   vue-cli · Generated "f7-vue-demo".

   To get started:

     cd f7-vue-demo
     npm install
     npm run dev

   Documentation can be found at https://vuejs-templates.github.io/webpack

2.1 添加Element 插件(Element很少支持移动端,放弃)

https://github.com/ElemeFE/element

http://element.eleme.io/#/zh-CN/component/quickstart

$ npm install element-ui -S

2.2 添加Mint UI插件(Mint组件太少,比如没有Card/Modal等,模板少,放弃)

http://mint-ui.github.io/#!/zh-cn

2.3. 添加Frameword7-Vue插件

http://framework7.io/vue/templates.html

F7提供了丰富的移动端插件,还有许多成型的模板,用F7+Vue应该是个很好的选择

安装教程参考: http://framework7.io/vue/templates.html
$ git clone https://github.com/nolimits4web/Framework7-Vue-Webpack-Template F7-Vue-Webpack-Demo
$ cd F7-Vue-Webpack-Demo
$ cnpm install
$ cnpm run dev

Project Structure

  • src/components - folder with custom .vue components
  • src/pages - app .vue pages
  • src/main.js - main app file where you include/import all required libs and init app
  • src/routes.js - app routes
  • src/app.vue - main app structure/component
  • dist/css - app styles, put custom app CSS styles here as well
  • dist/css/build.css - Vue components styles will be extracted here on npm run build

3. 修改代码

最后src/main.js的代码如下:

import Vue from 'vue'
import App from './App'
import Element from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
Vue.use(Element)

new Vue({
  el: '#app',
  template: '<App/>',
  components: { App }
})
 

src/app.vue的代码如下:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <hello></hello>
  </div>
</template>

<script>
import Hello from './components/Hello'

export default {
  name: 'app',
  components: {
    Hello
  }
}
</script>

components/Hello.vue的代码如下:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h2>Essential Links</h2>
    <el-button @click="visible = true">按钮</el-button>
    <el-dialog v-model="visible" title="Hello world">
      <p>欢迎使用 Element</p>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      visible: false
    }
  }
}
</script>

4. 运行

$  cnpm run dev

程序正常运行。

学习 Vue 是一个循序渐进的过程,建议从基础开始,逐步掌握其核心概念和高级特性。以下是学习 Vue 的分步指南: --- ### 第一步:了解 Vue 的基本概念 Vue 是一个用于构建用户界面的渐进式 JavaScript 框架。它的核心只关注视图层,易于上手,并且可以与其他库或项目集成。 #### 学习内容: - 什么是 Vue? - Vue 的特点:响应式数据绑定、组件化、指令、模板语法等。 - Vue 的生态系统(如 Vue Router、Vuex、Vue CLI 等)。 --- ### 第二步:搭建开发环境 1. 安装 Node.js 和 npm(Node.js 自带 npm)。 2. 使用 Vue CLI 创建项目: ```bash npm install -g @vue/cli vue create my-project cd my-project npm run serve ``` --- ### 第三步:掌握 Vue 基础语法 #### 1. 模板语法:插值、指令、事件绑定等。 ```vue <template> <div> <h1>{{ message }}</h1> <button @click="changeMessage">Change Message</button> </div> </template> <script> export default { data() { return { message: 'Hello Vue!' } }, methods: { changeMessage() { this.message = 'Message changed!'; } } } </script> ``` #### 2. 条件渲染与列表渲染: ```vue <template> <div> <p v-if="isVisible">This is visible</p> <ul> <li v-for="item in items" :key="item.id">{{ item.name }}</li> </ul> </div> </template> <script> export default { data() { return { isVisible: true, items: [ { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 3, name: 'Orange' } ] } } } </script> ``` --- ### 第四步:理解 Vue 组件系统 组件是 Vue 的核心概念之一,它允许你将 UI 拆分为独立、可复用的部分。 #### 示例:父子组件通信 ```vue <!-- ParentComponent.vue --> <template> <div> <ChildComponent :message="parentMessage" @child-event="handleChildEvent" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent' }; }, methods: { handleChildEvent(payload) { console.log('Received from child:', payload); } } } </script> ``` ```vue <!-- ChildComponent.vue --> <template> <div> <p>{{ message }}</p> <button @click="sendToParent">Send to Parent</button> </div> </template> <script> export default { props: ['message'], methods: { sendToParent() { this.$emit('child-event', 'Hello parent!'); } } } </script> ``` --- ### 第五步:学习 Vue 的响应式系统(Vue 3 Composition API) 在 Vue 3 中,推荐使用 Composition API 来组织逻辑。 ```vue <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> </div> </template> <script setup> import { ref } from 'vue'; const count = ref(0); function increment() { count.value++; } </script> ``` --- ### 第六步:使用 Vue Router 实现单页应用导航 安装 Vue Router: ```bash npm install vue-router@4 ``` ```js // router/index.js import { createRouter, createWebHistory } from 'vue-router'; import Home from '../views/Home.vue'; import About from '../views/About.vue'; const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router; ``` ```js // main.js import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; createApp(App).use(router).mount('#app'); ``` ```vue <!-- App.vue --> <template> <nav> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> </nav> <router-view /> </template> ``` --- ### 第七步:使用 Vuex 进行状态管理 Vuex 是 Vue 的状态管理模式和库,用于管理全局状态。 ```bash npm install vuex@next ``` ```js // store/index.js import { createStore } from 'vuex'; export default createStore({ state: { count: 0 }, mutations: { increment(state) { state.count++; } }, actions: { increment({ commit }) { commit('increment'); } }, getters: { count: state => state.count } }); ``` ```js // main.js import store from './store'; createApp(App).use(store).mount('#app'); ``` ```vue <!-- Component.vue --> <template> <div> <p>Count: {{ $store.state.count }}</p> <button @click="$store.dispatch('increment')">Increment</button> </div> </template> ``` --- ### 第八步:使用 Vue CLI 或 Vite 构建项目 - Vue CLI 是 Vue 官方的项目脚手架工具。 - Vite 是一个更快的现代前端构建工具,推荐用于 Vue 3 项目。 --- ### 第九步:部署你的 Vue 应用 构建生产版本: ```bash npm run build ``` 将生成的 `dist/` 文件夹上传到服务器即可部署。 --- ### 第十步:深入学习 Vue 高级特性 - 自定义指令 - 插件开发 - 渲染函数与 JSX - 异步组件 - 单元测试(使用 Jest / Vue Test Utils) - 性能优化技巧 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值