【问题标题】:Retrieving the url or route path within a Template检索模板中的 url 或路由路径
【发布时间】:2016-04-29 16:17:39
【问题描述】:

我目前正在使用 Iron Router。在我的模板文件中,我通过

检索 url
var url = Router.current().url

var url = Router.current().originalUrl

这些返回没有协议和主机名的当前路径。

/your/current/path

但是,当您刷新页面(浏览器刷新)时,相同的方法会返回整个 url(协议、主机名和路径)。

http://localhost:3000/your/current/path

如何可靠地检索整个 url 或路由路径?

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    您可以使用 Iron.Location 对象,因为这里有一些您可以使用的属性

    Iron.Location.get().rootUrl  --> 'http://localhost:3000'
    Iron.Location.get().path --> '/your/current/path'
    Iron.Location.get().port --> 3000
    Iron.Location.get().protocol --> 'http:'
    Iron.Location.get().queryObject ---> return query object
    Iron.Location.get().hostname --> 'localhost'
    Iron.Location.get().host --> 'localhost:3000'
    
    Meteor.absoluteUrl() will give the root url as well.
    

    【讨论】:

    • 感谢您的回复。如何导入Iron
    • 你用的是流星 1.3 吗?
    • 回答我自己的问题import { Iron } from 'meteor/iron:core';
    • 是的,这很酷,这就是您可以导入它的方式。这意味着您使用的是 1.3。
    • 我尝试运行您在 2 个用例中提到的所有命令:1)来自网站内的不同页面,所有命令都返回空,除了 Iron.Location.get().path 2)当网页刷新后,所有命令都返回一个值。要回答我的问题,哪个可靠,只有Iron.Location.get().path
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多