Get current route name in Ember

前端 未结 10 2131
我在风中等你
我在风中等你 2020-11-30 05:20

I need to get the current route name in my ember application; I tried this: Ember App.Router.router.currentState undefined but it doesn\'t work for me (there is probablig so

10条回答
  •  旧时难觅i
    2020-11-30 05:36

    You can simple parse the current URL. This way you can use your full url for example:

    http://127.0.0.1:8000/index.html/#/home
    

    and extract from this string the suffix:

    /home
    

    which is the current route name.

    A simple JS function (that works regardless to your Ember version) will be:

    function getCurrentRoute()
    {
        var currentRoute;
        var currentUrl = window.location.href; // 'http://127.0.0.1:8000/index.html/#/home'
    
        var indexOfHash = currentUrl.indexOf('#');
        if ((indexOfHash == -1) ||
            (indexOfHash == currentUrl.length - 1))
        {
            currentRoute = '/';
        }
        else
        {
            currentRoute = currentUrl.slice(indexOfHash + 1); // '/home'
        }
    
        return currentRoute;
    }
    

    Example of use:

    if (getCurrentRoute() == '/home')
    {
    // ...
    }
    

提交回复
热议问题