新手vue構(gòu)建單頁面應(yīng)用實例代碼
本文介紹了新手vue構(gòu)建單頁面應(yīng)用實例代碼,分享給大家,具體如下
步驟:
1.使用vue-cli創(chuàng)建項目
2.使用vue-router實現(xiàn)單頁路由
3.用vuex管理我們的數(shù)據(jù)流
4.使用vue-resource請求我們的node服務(wù)端
5.使用.vue文件進(jìn)行組件化的開發(fā)
一、目錄結(jié)構(gòu):

二、搭建項目
先安裝 vue-cli: sudo npm install -g vue-cli
使用vue-cli構(gòu)建初始化項目:vue init webpack project(創(chuàng)建webpack項目并下載依賴)
輸入命令進(jìn)入項目: cd my-project
安裝依賴: npm install
npm i
開始運(yùn)行: npm run dev (或輸入http://localhost:8080),在熱加載中運(yùn)行我們的應(yīng)用
它會去找到package.json的scripts對象,執(zhí)行node bulid/dev-server.js
在這文件里,配置了Webpack,會讓它去編譯項目文件,并且運(yùn)行服務(wù)器。
這些都準(zhǔn)備好后,我們需要為我們的路由、XHR請求、數(shù)據(jù)管理下載三個庫,我們可以從vue的官網(wǎng)中找到他們。另外我們使用bootstrap作為我的UI庫:
npm i vue-resource vue-router vuex bootstrap --save
三、項目開始
初始化項目(main.js)
查看我們的應(yīng)用文件,我們可以在src目錄下找到App.vue和main.js文件中,我們引入Vue和App,且創(chuàng)建了一個vue的實例(因為在router這行引入了App組件router.start(App,'#app'))
import Vue from 'vue'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
index.html
<body> <div id="app"></div> </body>
App.vue
<template>
<div id="app">
<div class="row">
<div class="col-xs-offset-2 col-xs-8">
<div class="page-header">
<h2>Router Basic - 01</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2 col-xs-offset-2">
<ul class="list-group">
<!--使用指令v-link進(jìn)行導(dǎo)航-->
<a class="list-group-item"><router-link to="/home">Home</router-link></a>
<a class="list-group-item"><router-link to="/about">About</router-link></a>
<a class="list-group-item"><router-link to="/contact">Contact</router-link></a>
</ul>
</div>
<div class="col-xs-6">
<div class="panel">
<div class="panel-body">
<!--用于渲染匹配的組件-->
<router-view></router-view>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'app'
}
</script>
src/components/Home.vue 作為我們的首頁
<template id="contact">
<div>
<h1>Home</h1>
<p>This is the tutorial about Contact.</p>
</div>
</template>
<script>
export default {
'/hello': 'Hello'
}
</script>
src/components/About.vue
<template id="about">
<div>
<h1>About</h1>
<p>This is the tutorial about vue-router.</p>
</div>
</template>
<script>
export default {
'/about': 'About'
}
</script>
src/components/Contact.vue
<template id="contact">
<div>
<h1>Contact</h1>
<p>This is the tutorial about Contact.</p>
</div>
</template>
export default {
'/contact': 'contact'
}
</script>
src/index.js
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/components/Hello'
import Home from '@/components/Home'
import About from '@/components/About'
import Contact from '@/components/Contact'
import 'bootstrap/dist/css/bootstrap.css'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Hello',
component: Hello
},
{
path: '/home',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
},
{
path: '/contact',
name: '/Contact',
component: Contact
}
]
})
spa地址:https://github.com/cinderellastory415/vue-demo/tree/master/spa
詳細(xì)操作:
git clone https://github.com/cinderellastory415/vue-demo/tree/master/spa
npm install
npm run dev
輸入以上命令,即可查看效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中動態(tài)路由加載組件,找不到module問題及解決
這篇文章主要介紹了vue中動態(tài)路由加載組件,找不到module問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
使用Vue寫一個todoList事件備忘錄經(jīng)典小案例
學(xué)習(xí)了幾天Vue之后終于迎來了第一個小案例,todoList是非常常見地一個小案例,下面這篇文章主要給大家介紹了關(guān)于使用Vue寫一個todoList事件備忘錄經(jīng)典小案例的相關(guān)資料,需要的朋友可以參考下2022-10-10
vue使用JSON編輯器:vue-json-editor詳解
文章介紹了如何在Vue項目中使用JSON編輯器插件`vue-json-editor`,包括安裝、引入、注冊和使用示例,通過這些步驟,用戶可以在Vue應(yīng)用中輕松實現(xiàn)JSON數(shù)據(jù)的編輯功能,文章最后呼吁大家支持腳本之家2025-01-01
Vue數(shù)據(jù)驅(qū)動試圖的實現(xiàn)方法及原理
當(dāng)Vue實例中的數(shù)據(jù)(data)發(fā)生變化時,與之相關(guān)聯(lián)的視圖(template)會自動更新,反映出最新的數(shù)據(jù)狀態(tài), Vue的數(shù)據(jù)驅(qū)動視圖是通過其響應(yīng)式系統(tǒng)實現(xiàn)的,以下是Vue數(shù)據(jù)驅(qū)動視圖實現(xiàn)的核心原理,需要的朋友可以參考下2024-10-10

