vue+vue-router+vuex+axios+php+mysql实现文章列表跳转

今天来和大家聊一下,vue项目中如何与后端php相结合。
目录结构
这里写图片描述

Home.vue

<template>
    <div>
        <homelist :list="list"></homelist>
    </div>
</template>

<script>
    import axios from 'axios'
    import homelist from "./pages/HomeList"
    export default {
        name:"Home",
        components:{
            homelist
        },
        computed:{
            list(){
                return this.$store.getters.list
            }
        },
        mounted(){
            this.$store.dispatch("getInfo")
        }
    }
</script>

<style>
</style>

HomeList.vue

<template>
    <div class="container">
        <div class="list-group">
          <router-link  class="list-group-item" v-for="item in list" :key="item.id" :to="'/detail/'+item.id">
            {{item.aram_title}}
          </router-link>
        </div>
    </div>
</template>

<script>
    export default {
        name:"HomeList",
        props:{
            list:Array
        },
        data(){
            return {

            }
        }
    }
</script>
<style>
</style>

HomeDetail.vue

<template>
    <div class="container">
        {{content}}
    </div>
</template>
<script>
    import axios from 'axios'
    export default {
        name:"HomeDetail",
        data(){
            return {
                content:""
            }
        },
//      this.$route.params.id
        mounted(){
            var _this = this
            axios.get("/api/detail.php",{
                params:{
                    id:this.$route.params.id
                }
            })
            .then(function(res){            
                var data = res.data
                _this.content = data[0].aram_content
            }).catch(function(res){ 
            })
        }
    }
</script>
<style scoped="scoped">
</style>

配置路由,router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import home from "@/components/Home"
import homedetail from "@/components/pages/HomeDetail"
Vue.use(Router)
export default new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: home,
    },
    {
      path:"/detail/:id",
      component:homedetail
    }
  ]
})

设置config/index.js

proxyTable: {
        "/api":{
            target:"http://localhost:80"
        }
    },

store/index.js

import Vue from 'vue'
import Vuex from "vuex"
import axios from 'axios'
Vue.use(Vuex)
const state = {
    list:[]
}
const mutations = {
    getInfo(state,data){
        state.list = data
    }
}
const actions = {
    getInfo(ctx){
        axios.get("/api/data.php")
            .then(function(res){
                var data = res.data
                ctx.commit("getInfo",data)
            }).catch(function(res){
                console.log(res)
            })
    }
}
const getters = {
    list(){
        return state.list
    }
}
export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters
})

这里写图片描述
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值