美文网首页亮宝前端学习室
Vue笔记 路由及数据请求

Vue笔记 路由及数据请求

作者: 路路有话说 | 来源:发表于2017-05-10 11:34 被阅读532次

路由 需要安装 路由的插件
数据请求心好累,一直请求不到 后来发现没引入 一定要记得引入
这里先做路由在做 数据请求

先做对应的安装

npm install  vue-router vue-resource --save-dev

对应的页面代码

import VueRouter from 'vue-router';
import VueResource from "vue-resource"
Vue.use(VueRouter);
Vue.use(VueResource);
//使用
const routerConfig = new VueRouter({
    routes:[
        { path: '/', component: news_list },
        { path: '/news', component: news_list },
        { path: '/news/:newsid', component: newsdetail,name:"newsdetail"},
        { path: '/login', component: loginView }
    ]
})

Vue.component('user-nav', user_nav)
let myvue=new Vue({
    el:".container",
    router:routerConfig,
    mounted(){
  this.$http.get("请求地址")
                    .then(function(res){
                      console.log(res.body)
                    },function(rs){})
     },});

说明

路由使用文档 在这里

启动路由

const routerConfig = new VueRouter({
    routes:[
        { path: '/', component: news_list },
        { path: '/news', component: news_list },
        { path: '/news/:newsid', component: newsdetail,name:"newsdetail"},
        { path: '/login', component: loginView }
    ]
})
const router = new VueRouter({ //初始化router
    routes:routesConfig
})
const myvue= new Vue({
    el:".container",
    router
});

设置头部-单独注册一个头组件

Vue.component("page-nav",pagenav);

数据请求使用文档在这里

文档中有一行奇怪的实例

this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);

someUrl 表示 请求地址
options 请求的参数
successCallback 请求成功的回调方法
errorCallback 请求失败的回调方法
我在上面就直接使用了function(rs){}
其中 rs 表示返回的值 可以直接拿来调用 ,这点上面的文档中有说明

需要注意的是 这里请求需要做跨域处理

在你的PHP端加入
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');   //允许所有
// 响应类型  
header('Access-Control-Allow-Methods:GET,POST');  
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type'); 

获取参数 假设我们的 url 是 http://localhost:8080/#/news/4
这里4 表示新闻的 id 可以使用
this.$route.params.newsid 获取到这个 4
对应在列表页中 a 标签 使用 <router-link :to="{name:'newsdetail', params:{ newsid: news.newsid}}"> 来替代
同样的文档 在这里
对应的照抄就行了

相关文章

  • Vue笔记 路由及数据请求

    路由 需要安装 路由的插件数据请求心好累,一直请求不到 后来发现没引入 一定要记得引入这里先做路由在做 数据请求 ...

  • Vue路由及默认路由的跳转

    https://router.vuejs.org/ 代码实现如下 Vue动态路由get传参 vue路由结合请求数据...

  • Vue应用

    Vue项目 Vue结构 Vue项目打包与发布 Vue语法二 Vue网络请求 Vue路由 动态路由 编程式路由导航

  • 电商管理项目

    前端技术栈 vue、vue-router(路由)、Element-UI、Axios(发起网络数据请求)、Echar...

  • vue常用的插件等安装

    Vuex:状态管理 vue-router:路由 axios:请求数据 mock:模拟后台数据 less和sass ...

  • Vue路由

    Vue路由 什么是Vue路由? 后端路由:即请求的URL地址都对应后端的接口,请求URL响应对应的服务器的资源。 ...

  • vue基础

    vue全家桶 vue-cli 框架 vue vuex 状态管理 vue-router 路由 axios 请求...

  • vue打包后出现空白,请求不到js数据问题---router 坑

    vue打包后出现空白,请求不到js数据问题 可能是路由模式有问题 如果路由模式是hash模式,头部携带#,则pub...

  • nodejs_路由

    路由:路径由来。 为路由提供请求的 URL 和其他需要的 GET 及 POST 参数,随后路由需要根据这些数据来执...

  • Vue 学习笔记 vue-router路由和前端状态管理

    Vue 学习笔记十一、vue-router路由和前端状态管理 什么是路由:网址 11.1 vue­-router路...

网友评论

    本文标题:Vue笔记 路由及数据请求

    本文链接:https://www.haomeiwen.com/subject/liwwtxtx.html