首頁 web前端 js教程 Vue+Vux專案(詳細教學)

Vue+Vux專案(詳細教學)

Jun 23, 2018 pm 03:04 PM
vue 專案

本文給大家分享一段詳細的程式碼給大家介紹Vue Vux專案實踐想法,需要的朋友可以參考下

提供完整的路由,services""""`

   - -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------

index.html

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
  <title>insurance-weixin</title>
 </head>
 <body>
  <p id="app-box"></p>
  <!-- built files will be auto injected -->
 </body>
</html>
登入後複製

   ---------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------

main.js

import Vue from &#39;vue&#39;
import Vuex from &#39;vuex&#39;
import VueRouter from &#39;vue-router&#39;
import FastClick from &#39;fastclick&#39;
import {WechatPlugin, AjaxPlugin, LoadingPlugin, ToastPlugin, AlertPlugin} from &#39;vux&#39;
import App from &#39;./app.vue&#39;
/**
 * 加载插件
 */
Vue.use(Vuex)
Vue.use(VueRouter)
Vue.use(WechatPlugin)
Vue.use(AjaxPlugin)
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)
/**
 * 定义常量
 */
const domainName = &#39;localhost:8010&#39;
const serverName = &#39;localhost:3000&#39;
const apiPrefix = serverName + &#39;/api/outer&#39;
const loginTimeOutErrorCode = &#39;login_timeout_error&#39;
/**
 * 设置vuex
 */
const store = new Vuex.Store({})
store.registerModule(&#39;vux&#39;, {
 state: {
  loading: false,
  showBack: true,
  title: &#39;&#39;
 },
 mutations: {
  updateLoading (state, loading) {
   state.loading = loading
  },
  updateShowBack (state, showBack) {
   state.showBack = showBack
  },
  updateTitle (state, title) {
   state.title = title
  }
 }
})
/**
 * 设置路由
 */
const routes = [
 // 初始页
 {
  path: &#39;/&#39;,
  component: function (resolve) {
   require([&#39;./components/init.vue&#39;], resolve)
  }
 },
 // 主页
 {
  path: &#39;/index&#39;,
  component: function (resolve) {
   require([&#39;./components/index.vue&#39;], resolve)
  },
  children: [
   // 测试页
   {
    path: &#39;test&#39;,
    component: function (resolve) {
     require([&#39;./components/tests/page.vue&#39;], resolve)
    }
   }
  ]
 },
 // 绑定页
 {
  path: &#39;/bind&#39;,
  component: function (resolve) {
   require([&#39;./components/bind.vue&#39;], resolve)
  }
 }
]
const router = new VueRouter({
 routes
})
router.beforeEach(function (to, from, next) {
 store.commit(&#39;updateLoading&#39;, true)
 store.commit(&#39;updateShowBack&#39;, true)
 next()
})
router.afterEach(function (to) {
 store.commit(&#39;updateLoading&#39;, false)
})
/**
 * 点击延迟
 */
FastClick.attach(document.body)
/**
 * 日志输出开关
 */
Vue.config.productionTip = true
/**
 * 定义全局公用常量
 */
Vue.prototype.domainName = domainName
Vue.prototype.serverName = serverName
Vue.prototype.apiPrefix = apiPrefix
/**
 * 定义全局公用方法
 */
Vue.prototype.http = function (opts) {
 let vue = this
 vue.$vux.loading.show({
  text: &#39;Loading&#39;
 })
 vue.$http({
  method: opts.method,
  url: apiPrefix + opts.url,
  headers: opts.headers || {},
  params: opts.params || {},
  data: opts.data || {}
 }).then(function (response) {
  vue.$vux.loading.hide()
  opts.success(response.data.data)
 }).catch(function (error) {
  vue.$vux.loading.hide()
  if (!opts.error) {
   let response = error.response
   let errorMessage = &#39;请求失败&#39;
   if (response && response.data) {
    if (response.data.code === loginTimeOutErrorCode) {
     window.location.href = &#39;/&#39;
    }
    errorMessage = response.data.message
   }
   vue.$vux.alert.show({
    title: &#39;提示&#39;,
    content: errorMessage
   })
  } else {
   opts.error(error.response.data.data)
  }
 })
}
Vue.prototype.get = function (opts) {
 opts.method = &#39;get&#39;
 this.http(opts)
}
Vue.prototype.post = function (opts) {
 opts.method = &#39;post&#39;
 this.http(opts)
}
Vue.prototype.put = function (opts) {
 opts.method = &#39;put&#39;
 this.http(opts)
}
Vue.prototype.delete = function (opts) {
 opts.method = &#39;delete&#39;
 this.http(opts)
}
Vue.prototype.valid = function (opts) {
 let vue = this
 let valid = true
 if (opts.ref && !opts.ref.valid) {
  valid = false
 }
 if (opts.ignoreRefs) {
  let newRefs = []
  for (let i in opts.refs) {
   let ref = opts.refs[i]
   for (let j in opts.ignoreRefs) {
    let ignoreRef = opts.ignoreRefs[j]
    if (ref !== ignoreRef) {
     newRefs.push(ref)
    }
   }
  }
  opts.refs = newRefs
 }
 for (let i in opts.refs) {
  if (!opts.refs[i].valid) {
   valid = false
   break
  }
 }
 if (valid) {
  opts.success()
 } else if (opts.error) {
  opts.error()
 } else {
  vue.$vux.toast.show({
   text: &#39;请检查输入&#39;
  })
 }
}
Vue.prototype.closeShowBack = function () {
 this.$store.commit(&#39;updateShowBack&#39;, false)
}
Vue.prototype.updateTitle = function (value) {
 this.$store.commit(&#39;updateTitle&#39;, value)
}
/**
 * 创建实例
 */
new Vue({
 store,
 router,
 render: h => h(App)
}).$mount(&#39;#app-box&#39;)
app.vue
<template>
 <p id="app">
  <loading v-model="isLoading"></loading>
  <transition>
   <router-view></router-view>
  </transition>
 </p>
</template>
<script>
 import {Loading} from &#39;vux&#39;
 import {mapState} from &#39;vuex&#39;
 export default {
  name: &#39;app&#39;,
  components: {
   Loading
  },
  computed: {
   ...mapState({
    isLoading: state => state.vux.isLoading
   })
  }
 }
</script>
<style lang="less">
 @import &#39;~vux/src/styles/reset.less&#39;;
 body {
  background-color: #fbf9fe;
 }
</style>
components/index.vue
<template>
 <p style="height:100%;">
  <top style="margin-bottom:46px"></top>
  <transition>
   <router-view></router-view>
  </transition>
  <bottom></bottom>
 </p>
</template>
<script>
 import Top from &#39;./layouts/top.vue&#39;
 import Bottom from &#39;./layouts/bottom.vue&#39;
 export default {
  components: {
   Top,
   Bottom
  }
 }
</script>
<style>
 html, body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
 }
</style>
components/tests/page.vue
<template>
 <p>
  <page @loadMore="loadMore" @refresh="refresh">
   <p>
    <p v-for="i in n">placeholder {{i}}</p>
   </p>
  </page>
 </p>
</template>
<script>
 import Page from &#39;../kits/page.vue&#39;
 import {cookie} from &#39;vux&#39;
 export default {
  components: {
   Page
  },
  created () {
   let vue = this
   vue.closeShowBack()
   vue.updateTitle(&#39;测试页面&#39;),
   //获取常量
    console.log(0)
   vue.get({
    url: &#39;/test/constants&#39;,
    headers: {
     &#39;token&#39;: cookie.get(&#39;token&#39;)
    },
    success: function (data) {
     cookie.set(&#39;constants&#39;,JSON.stringify(data),{
      expires: 1
     })
    }
   })
  },
  data () {
   return {
    n: 10,
   }
  },
  methods: {
   loadMore () {
    this.n += 10
   },
   refresh () {
    this.n = 10
   },
  }
 }
</script>
登入後複製

components/tests/page.vue程式碼中的import Page from '../kits/ page.vue'是我自己寫的下拉刷新上啦加在的元件,運行的話刪掉這些引用就可以了。

本次記錄摘要是從剛完成的專案中抽離的部分程式碼(註:本專案實踐程式碼,可運行,可運行,可運行,可運行)

#上面是我整理給大家的,希望今後對大家有幫助。

相關文章:

在微信小程式中有關功能函數總結(詳細教學)

在javaScript中如何使用手機號碼校驗工具類別PhoneUtils

在微信小程式中如何實作下載進度條

在微信小程式中如何使用video元件播放視頻

以上是Vue+Vux專案(詳細教學)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vue.js怎麼引用js文件 vue.js怎麼引用js文件 Apr 07, 2025 pm 11:27 PM

在 Vue.js 中引用 JS 文件的方法有三種:直接使用 &lt;script&gt; 標籤指定路徑;利用 mounted() 生命週期鉤子動態導入;通過 Vuex 狀態管理庫進行導入。

vue怎麼給按鈕添加函數 vue怎麼給按鈕添加函數 Apr 08, 2025 am 08:51 AM

可以通過以下步驟為 Vue 按鈕添加函數:將 HTML 模板中的按鈕綁定到一個方法。在 Vue 實例中定義該方法並編寫函數邏輯。

vue中的watch怎麼用 vue中的watch怎麼用 Apr 07, 2025 pm 11:36 PM

Vue.js 中的 watch 選項允許開發者監聽特定數據的變化。當數據發生變化時,watch 會觸發一個回調函數,用於執行更新視圖或其他任務。其配置選項包括 immediate,用於指定是否立即執行回調,以及 deep,用於指定是否遞歸監聽對像或數組的更改。

vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

vue返回上一頁的方法 vue返回上一頁的方法 Apr 07, 2025 pm 11:30 PM

Vue.js 返回上一頁有四種方法:$router.go(-1)$router.back()使用 &lt;router-link to=&quot;/&quot;&gt; 組件window.history.back(),方法選擇取決於場景。

Vue 實現跑馬燈/文字滾動效果 Vue 實現跑馬燈/文字滾動效果 Apr 07, 2025 pm 10:51 PM

在 Vue 中實現跑馬燈/文字滾動效果,可以使用 CSS 動畫或第三方庫。本文介紹了使用 CSS 動畫的方法:創建滾動文本,用 &lt;div&gt; 包裹文本。定義 CSS 動畫,設置 overflow: hidden、width 和 animation。定義關鍵幀,設置動畫開始和結束時的 transform: translateX()。調整動畫屬性,如持續時間、滾動速度和方向。

怎樣查詢vue的版本 怎樣查詢vue的版本 Apr 07, 2025 pm 11:24 PM

可以通過以下方法查詢 Vue 版本:使用 Vue Devtools 在瀏覽器的控制台中查看“Vue”選項卡。使用 npm 運行“npm list -g vue”命令。在 package.json 文件的“dependencies”對像中查找 Vue 項。對於 Vue CLI 項目,運行“vue --version”命令。檢查 HTML 文件中引用 Vue 文件的 &lt;script&gt; 標籤中的版本信息。

vue遍歷怎麼用 vue遍歷怎麼用 Apr 07, 2025 pm 11:48 PM

Vue.js 遍歷數組和對像有三種常見方法:v-for 指令用於遍歷每個元素並渲染模板;v-bind 指令可與 v-for 一起使用,為每個元素動態設置屬性值;.map 方法可將數組元素轉換為新數組。

See all articles