简体   繁体   English

在VueJS中,如何根据我的JS中填充的内容在视图中使用load?

[英]In VueJS, how do I use load in a view based on what is populated in my JS?

I am new to Vue.JS and require a little help as I am stuck with some logic. 我是Vue.JS的新手,需要一些帮助,因为我坚持一些逻辑。 I have been given a project to building and as I am under NDA I can only show comparative code examples. 我已经获得了一个建设项目,因为我在NDA下,我只能展示比较代码示例。

In one View (Bars), which is brought into the viewport of the application using the router, I have a simple for loop. 在一个使用路由器进入应用程序视口的视图(Bars)中,我有一个简单的for循环。

<ul class="bar-listings">
  <li v-for="bar in bars">
      <router-link to="{bar.barPage}">
          <div class="item">
             <div class="item-bd">
                 <h2>{{ bar.barName }}</h2>
             </div>
          </div>
      </router-link>
  </li>
</ul> 

Then in my JS file conatining all my data, I have this (Only one object for now) 然后在我的JS文件中包含我的所有数据,我有这个(现在只有一个对象)

export default [
  {
    barName: "The Tropicana Bar",
    barPage: "views/bars/Tropicana",
  }
];

The title displays correctly on Bars so the loop is pulling the data correctly. 标题在Bars上正确显示,因此循环正确地提取数据。

However, I will have a .vue file for each bar, which also uses the data from my JS file. 但是,我将为每个条形图提供一个.vue文件,该文件也使用我的JS文件中的数据。 See below: 见下文:

<template>
    <div class="content-wrapper">
        <h1>{{ getBarByIndex({ bars, index }).barName }}</h1>
    </div>
</template>

<script>
import bars from "./../../data/bars";
export default {
  data() {
    return {
      bars: bars
    };
  },
  methods: {
  getBarByIndex({ bars = [], index = 0 }) {
    return bars[index] || {}
  }
}
};
</script>

So what I need to solve is how do I make the <a v-bind:href=""> load the view for this bar? 所以我需要解决的是如何使<a v-bind:href="">加载此栏的视图?

What I think you can do, is to have a child component inside your component, you can leverage this by using nested routes in your router-view, additionally, you can pass props as an object to your router components, parent or child, whatever you need. 我认为你可以做的是在你的组件中有一个子组件,你可以通过在路由器视图中使用嵌套路由来利用它,另外,你可以将道具作为对象传递给路由器组件,父或子,等等你需要。

Basically, you will have a main router view, but inside your component, you will have another router-view which renders child components. 基本上,您将拥有一个主路由器视图,但在组件内部,您将拥有另一个呈现子组件的路由器视图。

See: Nested Routes 请参阅:嵌套路由

See: Passing Props to Route components 请参阅:将道具传递给路线组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VueJS:如何在模板中使用main.js中的数据? - VueJS: How do I use data from main.js in my templates? 如何在骨干网视图中使用“ this”? - How do I use “this” in my Backbone View? 如何使用过滤器根据我使用 React Js 在日期选择器上选择的日期返回所有值 - How to use the filter to return all the values based on what date I choose on my datepicker using React Js 如何在创建VueJS实例之前加载外部托管(CDN)JS库*? - How do I load an externally hosted (CDN) JS library *before* the VueJS instance is created? 如何将外部.js文件添加到index.html? (VueJS2) - How do I add an external .js file to my index.html? (VueJS2) 我的 HTML 和 CSS 使用 Sublime,但现在我需要使用 JS,我该怎么办? - I use Sublime for my HTML and CSS, but now I need to use JS, what do I do? 如何将我的脚本加载到 node.js REPL 中? - How do I load my script into the node.js REPL? Angular JS:如何延迟加载指令? - Angular JS : How do I Lazy load my directive? 如何使用jquery验证动态填充的表单字段? - How do I use jquery to validate form fields, that was populated dynamically? 如何在此js文件中使用Jquery的加载功能? - How do I use Jquery's load function in this js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM