简体   繁体   English

Vue:使用 router.resolve 进行路由导航(在新选项卡中打开页面),参数中的数据丢失

[英]Vue: route navigation with router.resolve (to open a page in new tab) with data in params is lost

I have a component, lets call component 1. A method in component1 makes an axios.post request and the server returns a bunch of data.我有一个组件,让我们调用组件 1。component1 中的一个方法发出axios.post请求,服务器返回一堆数据。 When data is loaded, a new button appears.加载数据后,会出现一个新按钮。 When this button is clicked, it will be navigated to another route with another component, let call this component2.单击此按钮时,它将导航到另一个带有另一个组件的路由,让我们调用这个组件2。 Now some of the loaded data from component1 needs to transferred to component2 and should be opened in new tab.现在,从 component1 加载的一些数据需要传输到 component2 并且应该在新选项卡中打开。 Below is the code:下面是代码:

<script>
import axios from 'axios';
export default {
    name: "CheckStandard",
    data() {
        return {
            standard: '',
            time: {},
            programs: {},
            example: {},
        }
    },
    methods: {
        checkData(){
            let std= {
                std: this.standard,
            }
            axios.post('http://localhost:3000/postdata', std)
            .then(res => {
                if (res.status === 200) {
                    if (res.data === 0) {
                        this.invalidID = "This Standard does not exist"
                    }
                    else {
                       
                       let data = res.data
                       
                       this.time = res.data["Starttime"];
                       this.programs = res.data["program"]  
                       this.example = res.data["example"]  
                }

            }).catch(error => {
                console.log(error);
                this.error = error.response
            })
        },

}
 goToPictures(){
           let route = this.$router.resolve({
                name:'ProgramCheckList',
                params: {
                    programs: this.programs,
                    time: this.time,
                    example: this.example
                }
            })
            window.open(route.href,'_blank')
        },
    }
}
</script>

The function goToPictures is the function that is invoked after clicking the button. function goToPictures是单击按钮后调用的 function。 Now in this function goToPictures I have defined the route to navigate to another tab.现在在这个 function goToPictures中,我定义了导航到另一个选项卡的路线。 But the problem the data in the params which it should carry is lost.但是问题是它应该携带的params中的数据丢失了。 I tried with $router.push ofcourse it works but it is not to open in new tab.我尝试使用$router.push它可以工作,但它不能在新标签中打开。 Below is the code for the same:下面是相同的代码:

    goToPictures(){
       this.$router.resolve({
            name:'ProgramCheckList',
            params: {
                programs: this.programs,
                time: this.time,
                example: this.example
            }
        })
    },
}

Since I am new to vue, I have tried my best to look for an answer for this, even I have came across some posts in several forums mentioning, it is may be not be possible even, instead advised to use vuex.由于我是 vue 新手,因此我已尽力寻找答案,即使我在几个论坛中遇到过一些帖子提到,甚至可能不可能,而是建议使用 vuex。 But I still wanted to post it, maybe we have a solution now or any other idea.但我仍然想发布它,也许我们现在有解决方案或任何其他想法。 Thanks谢谢

The problem you're seeing stems from the fact that, when you open a new window, Vue is basically going to re-render your components as if you hit refresh.您看到的问题源于这样一个事实:当您打开一个新的 window 时,Vue 基本上会重新渲染您的组件,就像您点击刷新一样。 Your Component 2 has props that it can only inherit from another component, and as such, it has no possible way of knowing what the props it needs to use are.您的Component 2具有只能从另一个组件继承的 props,因此,它无法知道它需要使用的 props 是什么。

To illustrate in simple terms what's happening:为了简单地说明正在发生的事情:

The user navigates to Component 1 .用户导航到Component 1 They click the button, which makes the GET request.他们单击按钮,发出 GET 请求。 You now have some data that you can pass onto Component 2 as props.您现在有一些数据可以作为道具传递给Component 2

In a regular environment, the user would simply click on the link leading to Component 2, and the props would be passed on normally, and everything would work as intended.在常规环境中,用户只需单击指向组件 2 的链接,道具就会正常传递,一切都会按预期工作。

The problem in your situation is that Component 2 depends on Component 1 for its data.您遇到的问题是Component 2的数据依赖Component 1 By navigating directly to the Component 2 route (in this situation, opening a new window is functionally identical to a user copy/pasting the url into the adress bar), Vue never has the chance of interacting with Component 1 , and never gets told where to get the props it needs to populate Component 2 .通过直接导航到Component 2路线(在这种情况下,打开一个新的 window 在功能上与用户将 url 复制/粘贴到地址栏相同),Vue 永远没有机会与Component 1交互,并且永远不会被告知在哪里获取填充Component 2所需的道具。

Overcoming the issue克服问题

There's a few things you can do here to overcome this issue.您可以在这里做一些事情来克服这个问题。 The most obvious one is to simply open Component 2 as you would normally, without opening a new window, but keep in mind that even if you do this, should a user copy/paste the URL where Component 2 is, they'll run into the exact same issue.最明显的方法是像往常一样简单地打开Component 2 ,而不打开新的 window,但请记住,即使您这样做,如果用户复制/粘贴Component 2所在的 URL,他们会遇到完全相同的问题。

To properly deal with the issue, you have to specify a way for Component 2 to grab the data it needs.要正确处理该问题,您必须Component 2指定一种获取所需数据的方法。 Since the data is already fetched, it makes sense to do this in the created() or mounted() hooks, though if you wanted to you could also deal with this in Vue Router's beforeRouteEnter() hook.由于数据已经获取,因此在created()mounted()钩子中执行此操作是有意义的,但如果您愿意,您也可以在 Vue Router 的beforeRouteEnter()钩子中处理此问题。

Vuex Vuex

While you don't necessarily need a state management tool like Vuex, it's probably the simplest way for your needs.虽然您不一定需要像 Vuex 这样的 state 管理工具,但它可能是满足您需求的最简单方法。 When you grab the data from Component 1 , store it and access it from the Component 2 mounted() hook.当您从Component 1获取数据时,将其存储并从Component 2 mounted()挂钩访问它。 Easy-peasy.十分简单。

localStorage()

Alternatively, depending on how the data is being served, you could use localStorage() .或者,根据提供数据的方式,您可以使用localStorage() Since you're opening a new window, sessionStorage() won't work.由于您要打开一个新的 window,因此sessionStorage()将不起作用。 Do note that localStorage() can only hold strings and nothing else, and isn't necessarily available in every browser.请注意localStorage()只能保存字符串而不能保存其他任何内容,并且不一定在每个浏览器中都可用。

You can store the data to a global variable and use that in the newly opened window.您可以将数据存储到全局变量中,并在新打开的 window 中使用它。 Asked here Can I pass a JavaScript variable to another browser window?在这里问我可以将 JavaScript 变量传递给另一个浏览器 window 吗?

Provided the windows are from the same security domain, and you have a reference to the other window, yes.如果 windows 来自同一个安全域,并且您有对其他 window 的引用,是的。

Javascript's open() method returns a reference to the window created (or existing window if it reuses an existing one). Javascript 的 open() 方法返回对创建的 window 的引用(或现有的 window,如果它重用现有的 Z05B8C702FBF4Z)。 Each window created in such a way gets a property applied to it "window.opener" pointing to the window which created it.以这种方式创建的每个 window 都会应用一个属性“window.opener”,指向创建它的 window。

Either can then use the DOM (security depending) to access properties of the other one, or its documents,frames etc.然后可以使用 DOM(取决于安全性)来访问另一个的属性,或者它的文档、框架等。

Another example from same thread:来自同一线程的另一个示例:

var thisIsAnObject = {foo:'bar'};
var w = window.open("http://example.com");
w.myVariable = thisIsAnObject;
//or this from the new window
var myVariable = window.opener.thisIsAnObject;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM