【问题标题】:how to use a path twig in vueJS with Proyect Symfony - how to correctly use FSRouting如何在带有 Proyect Symfony 的 vueJS 中使用路径树枝 - 如何正确使用 FSRouting
【发布时间】:2019-10-26 13:32:39
【问题描述】:

我有一个Vue 代码,但我需要在twig 中显示一个链接:

<template>
<div class="col t-blk text-center d-flex justify-content-around">                                                       
<a class="btn btn-primary btn-md border-r-0 d-table py-2 px-4" href="{{path('corpasesoria')}}">Con Asesor</a>
<a class="btn btn-primary btn-md border-r-0 d-table py-2 px-4" id="step01-tab" data-toggle="tab" href="#step01" role="tab" aria-controls="step01" aria-selected="false">Sin Asesor</a>
</div>
</template>

但是在编译webpack的时候,twig的链接报错,无法推进。

这是错误:

ERROR  Failed to compile with 1 errors                                                                     00:58:12

 error  in ./assets/components/Corporativo/Corporativo.vue?vue&type=template&id=270a4b6a&scoped=true&

Module Error (from ./node_modules/vue-loader/lib/loaders/templateLoader.js):
(Emitted value instead of an instance of Error) 

  Errors compiling template:

  href="{{path('corpasesoria')}}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">.

  15 |                                                                                          <div class="row">
  16 |                                                                                                  <div class="col t-blk text-center d-flex justify-content-around">
  17 |                                                                                                          <a class="btn btn-primary btn-md border-r-0 d-table py-2 px-4" href="{{path('corpasesoria')}}">Con Asesor</a>
     |                                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  18 |                                                                                                          <a class="btn btn-primary btn-md border-r-0 d-table py-2 px-4" id="step01-tab" data-toggle="tab" href="#step01" role="tab" aria-controls="step01" aria-selected="false">Sin Asesor</a>
  19 |                                                                           

你有什么想法吗?

【问题讨论】:

  • 错误是什么?
  • 这是错误:href="{{path('corpasesoria')}}":属性内的插值已被删除。请改用 v-bind 或冒号简写。例如,使用
    代替

标签: symfony webpack vuejs2 twig


【解决方案1】:

这是一个例子

import RoutingData from '../../../../dist/js/fos_js_routes';

从'fos-routing'导入路由;

Routing.setData(RoutingData); 导出默认路由;

【讨论】:

  • 现在。我配置了所有但这是错误:“错误:”路由“corpasesoria”不存在。””在vuejs中,这是代码:方法:{conAsesor:函数(事件){警报('Hello!')路由。生成(“尸体”); }} 有什么问题?
  • 每次添加新路线时都需要转储路线,如symfony.com/doc/master/bundles/FOSJsRoutingBundle/usage.html所述
  • 是的,这是我控制器中的路由: /** * @Route("/corporativo/asesoria", * options = { "expose" = true }, * name = "corpasesoria", * ) */ 有什么问题??
  • 谢谢。现在一切都很完美。我要更正答案
【解决方案2】:

你可以尝试使用 fosjsrouring bundle 在 js 中生成链接 https://symfony.com/doc/master/bundles/FOSJsRoutingBundle/index.html

【讨论】:

  • 我在 twig 中使用这种形式: Routing.generate('your_route_with_params', { param: varJavascript });如何在 VueJS 中使用这个路由???
  • 在任何js环境下使用都是一样的。
【解决方案3】:

感谢slmder-h的帮助 首先你需要安装:npmjs.com/package/fos-routing

npm install fos-routing --save

那你需要阅读 symfony 文档:https://symfony.com/doc/master/bundles/FOSJsRoutingBundle/usage.html

在终端之后

# Symfony Flex
bin/console fos:js-routing:dump --format=json --target=public/js/fos_js_routes.json

在 .vue 或 .js 文件中

const routes = require ('../../ public / js / fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';

Routing.setRoutingData (routes);
Routing.generate ('asesoriajson');

最后是控制器:

/**
 * @Route("/corporativo/asesoriajson",
 *     options = { "expose" = true },
 *     name = "asesoriajson",
 *       
 * )
 */
 public function asesoriajson(Request $request, SendMail $Sendmail)
 {
    $response = array();
    $response['type'] = null;
    $response['title'] = null;
    $response['message'] = null;

    $response['type'] = 'success';
    $response['title'] = 'Success';
    $response['message'] = 'Usted decidio utilizar un asesor'; 
    return new JsonResponse($response, 200);                    
}

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签