vue Blog 学习笔记 (3) 路由和组件加载关系

a 链接跳转
this.$router.push('/dashboard/home')

get 从接口获取数据
this.$http.get('comment/' + this.$route.params.id + '/edit')

post 提交表单
this.$http.post('comment/' + this.$route.params.id, this.comment)

this.$route.params.id
You can have multiple dynamic segments in the same route, and they will map to corresponding fields on $route.params. Examples:

pattern matched path $route.params
/user/:username /user/evan { username: 'evan' }
/user/:username/post/:post_id /user/evan/post/123 { username: 'evan', post_id: 123 }

后台 vue 结构

webpack.mix.js 定义别名
resolve: {
    alias: {
      'components': 'assets/js/components',
      'config': 'assets/js/config',
      'lang': 'assets/js/lang',
      'plugins': 'assets/js/plugins',
      'vendor': 'assets/js/vendor',
      'views': 'assets/js/views',
      'dashboard': 'assets/js/views/dashboard',
    },
    modules: [
      'node_modules',
      path.resolve(__dirname, "resources")
    ]
  },

后台入口
resources/views/dashboard/index.blade.php
  引用 <script src="{{ mix(js/app.js) }}"></script>


app.js 文件中
  import App from './App.vue'

 Vue.use 使用全局组件
  Vue.use(httpPlugin);
  Vue.use(VueI18n);
  Vue.use(VueRouter);

  Vue.component() 构建组件,使用自定义组件
  Vue.component(
    'vue-table-pagination',
    require('components/dashboard/TablePagination.vue')
  );
  Vue.component(
    'vue-table',
    require('components/dashboard/Table.vue')
  );
  Vue.component(
    'vue-form',
    require('components/dashboard/Form.vue')
  );
  

路由和模板

resources/assets/js/App.vue
<template>
  <router-view></router-view>
</template>
router-view

resources/assets/js/routes.js
后台路由,定义了后台的路由和模板
import Dashboard from 'views/Dashboard.vue'
import Parent from 'views/Parent.vue'

{
  path: 'comments',
  component: Parent,
  children: [
    {
      path: '/',
      component: () => import('dashboard/comment/Comment.vue')
    },
    {
      path: ':id/edit',
      component: () => import('dashboard/comment/Edit.vue')
    }
  ]
},

resources/assets/js/views/dashboard/comments/Comment.vue
<template>
  <div class="row">
    <vue-table :title="$t('page.comments')" :fields="fields" api-url="commentc" 
        show-paginate @table-action="tableActions">
    </vue-table>
  </div>
</template>

App.vue
Dashboard.vue
Parent.vue
Comment.vue
这几个组件文件,是怎么组合起来使用的呀?
通过 routes.js 吗, 路由中的 component: Parent 什么作用?删掉后,页面数据为空,也不报错

把首页的路由调整一下
如果写成
{
  path: 'home',
  component: () => import('dashboard/Home.vue')
}
或者
{
  path: 'home',
  component: Parent,
  children: [
    {
      path: '/',
      component: () => import('dashboard/Home.vue')
    }
  ]
}
这两种写法都是正常的,页面不报错,数据也是正常显示的

如果写成
{
  path: 'home',
  children: [
    {
      path: '/',
      component: () => import('dashboard/Home.vue')
    }
  ]
}
页面不报错,就是数据不显示



VUE forum 上看到一个讨论,给出了嵌套children 和 分开写路由的两个例子,觉得挺好的
嵌套的路由,父级路由里面还是要有 component 选项,譬如该项目的 Parent.vue 文件,虽然就 3 行,命名规则还是需要
<template>
  <router-view></router-view>
</template>


var router = new VueRouter({
  routes: [{
    path: '/',
    name: 'home',
    component: Home
  }, {
    path: '/dashboard/:slug',
    name: 'dashboard',
    props: true,
    component: Dashboard
  }, {
    path: '/dashboard/:slug/profile',
    name: 'profile',
    props: true,
    component: Profile
  }, {
    path: '/dashboard/:slug/settings',
    name: 'settings',
    props: true,
    component: Settings
  }]
});

var router = new VueRouter({
  routes: [{
    path: '/',
    name: 'home',
    component: Home
  }, {
    path: '/dashboard/:slug',
    component: DashboardRoot,
    children: [{
      path: '/',
      name: 'dashboard',
      props: true,
      component: Dashboard
    }, {
      path: 'profile',
      name: 'profile',
      props: true,
      component: Profile
    }, {
      path: 'settings',
      name: 'settings',
      props: true,
      component: Settings
    }]
  }]
});

Failed to mount component: template or render function not defined.

Your HTML in a single file component needs to be wrapped in template tags.
Component template should contain exactly one root element

Failed to mount component: template or render function not defined. (found in root instance)

ìndex.html contains a template in the DOM:

<div id="div">
  {{message}}
</div>

it's not explicitly defined as a template, like what you probably think of:

<template>
  <div id="div">
    {{message}}
  </div>
</template>

But it's still HTML that Vue has to convert into a render function in order to work. The render function would look like this:

render(h) {
  return h('div', {attrs: {id: 'app'}, this.message}
}

Outside of single file components (for which vue-loader compiles the render functions from the templates in the build process of your app), this compilation can only be done during runtime, in the browser, and this requires the standalone version of Vue.

How to create a comment system with Laravel 5.3 and Vue.Js

<template>
  <div class="container">
    <h4>Add Comment</h4>
    <form action="" @submit.prevent="edit ? editComment(comment.id) : createComment()">
      <div class="input-group">
        <textarea name="body" v-model="comment.body" ref="textarea" class="form-control"></textarea>
      </div>
        <div class="input-group">
          <button type="submit" class="btn btn-primary" v-show="!edit">Add Comment</button>
          <button type="submit" class="btn btn-primary" v-show="edit">Edit Comment</button>
        </div>
    </form>
    <h4>Comments</h4>
      <ul class="list-group">
        <li class="list-group-item" v-for="comment in comments">
          {{comment.body}}
          <a class="btn btn-default" v-on:click=" showComment(comment.id)">Edit</a>
          <a class="btn btn-danger" v-on:click=" deleteComment(comment.id)">Delete</a>
        </li>
      </ul>
  </div>
</template>

@submit.prevent: The @ symbol is equivalent to on:submit. Using submit.prevent we prevent the default action, which is to submit the form for the URL in the action attribute. Instead, we send the form to the one of the defined functions.

edit ? editComment(comment.id) : createComment(): Here we have two functions in a ternary operation. If the edit property is true, send the submited data to editComment(). If not, send do createComment(). Both these functions, as well the edit value, will be set in the script section below.

v-show="!edit" & v-show="edit": v-show enables the toggle property in the element. In this case, the element will be toggled if the edit variable is false. In the same way, by setting v-show=”edit” in the element below we indicate that we want to display the element when the edit variable is true


  • [Vue warn]: Error in event handler for "table-action": "TypeError: this.$router.delete is not a function"
    this.$router.push 链接跳转
    this.$http.delete 请求 PHP 获取数据
    • resources\assets\js\config\base.js
      export const apiUrl = '/api/'
    • resources\assets\js\plugins\http\index.js
      import axios from 'axios'
      import { apiUrl } from 'config/base'
      export const http = axios.create({
      baseUrl: apiUrl,
      })

this.$route.params.id vue 从当前的 url 获取参数值

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容