vue.js單頁面應(yīng)用實(shí)例的簡單實(shí)現(xiàn)
一:npm的安裝
由于新版的node.js已經(jīng)集成了npm的環(huán)境,所以只需去官網(wǎng)下載node.js并安裝,安裝完成后使用cmd檢測是否成功。
測試node的版本號:node -v
測試npm的版本號:npm -v
以上提示代表安裝成功
二:vue.js環(huán)境搭建
1、首先安裝淘寶的npm鏡像:npm install -g cnpm --registry=https://registry.npm.taobao.org
2、安裝vue.js環(huán)境::cnpm install -g vue-cli
3、測試vue的安裝:vue
三:vue.js項(xiàng)目的建立
新建一個名為pt的vue項(xiàng)目:在F盤創(chuàng)建一個名為pt的文件夾:執(zhí)行:cd f:\ vue init webpack pt
接下來會依次出現(xiàn)以下的操作
注:Use ESlint to lint your code-是否使用ESlint(最后選否,否則不熟悉這種嚴(yán)格的方式,會被坑慘,沒空格會報錯,多空格也會報錯)
vue項(xiàng)目的啟動步驟:(1)cd pt (2)npm install (3)npm run dev
最終的目錄結(jié)構(gòu):
四:創(chuàng)建一個vue實(shí)例
main.js:應(yīng)用入口文件
App.js:初始化組件
例:我們要實(shí)現(xiàn)如下效果的一個網(wǎng)站
有四個模塊:首頁、公司介紹、招賢納士、易點(diǎn)咨詢。
項(xiàng)目的思維導(dǎo)向圖:
1、配置入口文件main.js
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' // 引入router路由 import Router from 'vue-router' // 引入項(xiàng)目的四個模塊組件 import introduce from './components/introduce' import home from './components/home' import employment from './components/employment' import consult from './components/consult' // 使用router Vue.use(Router) // 定義路由 var routes = [{ path: '/home', component: home }, { path: '/introduce', component: introduce }, { path: '/employment', component: employment }, { path: '/consult', component: consult }] // 實(shí)例化路由 var vueRouter = new Router({ routes }) // 創(chuàng)建和掛載根實(shí)例 new Vue({ el: '#app', router: vueRouter, template: '<App></App>', components: { App } })
2、初始化組件App.vue開發(fā)
<template> <div id="app"> <div class="nav-top"> <!-- 引入公用的頭部 header組件 --> <v-header></v-header> </div> <div class="banner"> </div> <div class="contianer"> <!-- 路由中的幾個組件在這里被渲染,默認(rèn)被渲染的為第一個組件,也就是home組件 --> <router-view></router-view> </div> </div> </template> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; } .nav-top { position: absolute; top: 0; left: 50%; margin-left: -600px; z-index: 99; } .banner{ width: 100%; height: 370px; overflow: hidden; background: url("components/banner.jpg"); background-repeat: no-repeat; } </style> <script> //引入header組件 import header from './components/header.vue' //輸出header組件 export default{ components: { 'v-header': header } } </script>
3、創(chuàng)建公用頭部組件
<template> <div class="header"> <div class="header-wrapper"> <div class="logo"> <a href="/home" rel="external nofollow" ><img src="../assets/ysh.png" alt width="210"></a> </div> <ul class="nav"> <li><router-link to="/home">首頁</router-link></li> <li><router-link to="/introduce">公司介紹</router-link></li> <li><router-link to="/employment">招賢納士</router-link></li> <li><router-link to="/consult">易點(diǎn)咨詢</router-link></li> </ul> </div> </div> </template> <style> .header{ width:1200px; height:100px; margin:0 auto; color:#fff; } .header-wrapper{ width:1200px; height:100px; } .logo{ width:210px; height:100px; float:left; } .nav{ width:700px; height:100px; font-size:15px; float:right; } .nav li{ float:left; margin-right:30px; height:34px; line-height:34px; overflow:hidden; margin-top:34px; } .nav li:last-child{ margin-right:0; } .nav a{ display:inline-block; padding:0 13px; color:#fff; border-radius:15px; } .nav a.router-link-active{ background:#c10514; } </style>
4、創(chuàng)建其他組件
需注意模板文件都只能有一個根元素。
<template> <div class="intro"> 公司介紹 </div> <div> zx </div> </template> <style> .intro{ font-size:20px; color:#000; margin:20px auto; } </style>
像這種情況會報錯。
正確的為:
<template> <div class="intro"> 公司介紹 </div> </template> <style> .intro{ font-size:20px; color:#000; margin:20px auto; } </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解
這篇文章主要介紹了優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12vue循環(huán)el-button實(shí)現(xiàn)點(diǎn)擊哪個按鈕,那個按鈕就變色
這篇文章主要介紹了vue循環(huán)el-button實(shí)現(xiàn)點(diǎn)擊哪個按鈕,那個按鈕就變色問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10Vue3 實(shí)現(xiàn)雙盒子定位Overlay的示例
這篇文章主要介紹了Vue3 實(shí)現(xiàn)雙盒子定位Overlay的示例,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-12-12Vue如何動態(tài)修改el-table的某列數(shù)據(jù)
這篇文章主要介紹了Vue如何動態(tài)修改el-table的某列數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue中手機(jī)號,郵箱正則驗(yàn)證以及60s發(fā)送驗(yàn)證碼的實(shí)例
下面小編就為大家分享一篇vue中手機(jī)號,郵箱正則驗(yàn)證以及60s發(fā)送驗(yàn)證碼的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03element ui loading加載開啟與關(guān)閉方式
這篇文章主要介紹了element ui loading加載開啟與關(guān)閉方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08