當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Embeer.js RouterService rootURL用法及代碼示例


Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,它被大量網站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

RouteService 類的 rootURL 屬性由 Ember.js 使用來指定應用程序的基本 URL。

句法:

rootURL : UrlName ; 

值:

  • UrlName: 它是基本 URL 的名稱。

安裝和運行 Ember.js 的步驟:

第 1 步:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:

npm install ember-cli

第 2 步:現在您可以通過輸入以下代碼來創建項目:

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

示例 1:鍵入以下代碼以生成本示例的路由:

ember generate route file1

應用程序/配置/環境.js

Javascript


'use strict'; 
  
module.exports = function (environment) { 
    let ENV = { 
        modulePrefix: 'hello', 
        environment, 
  
        // Using '/' as base url for application 
        rootURL: '/', 
        locationType: 'auto', 
        EmberENV: { 
            FEATURES: { 
                // Here you can enable experimental features 
                // on an ember canary build 
                // e.g. EMBER_NATIVE_DECORATOR_SUPPORT: true 
            }, 
            EXTEND_PROTOTYPES: { 
  
                // Prevent Ember Data from  
                // overriding Date.parse. 
                Date: false, 
            }, 
        }, 
  
        APP: { 
            // Here you can pass flags/options  
            // to your application instance 
            // when it is created 
        }, 
    }; 
  
    if (environment === 'development') { 
    } 
  
    if (environment === 'test') { 
  
        // Testem prefers this... 
        ENV.locationType = 'none'; 
          
        // keep test console output quieter 
        ENV.APP.LOG_ACTIVE_GENERATION = false; 
        ENV.APP.LOG_VIEW_LOOKUPS = false; 
  
        ENV.APP.rootElement = '#ember-testing'; 
        ENV.APP.autoboot = false; 
    } 
  
    if (environment === 'production') { 
  
        // Here you can enable a 
        // production-specific feature 
    } 
  
    return ENV; 
};

應用程序/路線/file1.js

Javascript


import Route from '@ember/routing/route'; 
  
export default Route.extend({ 
    model() { 
        return 'RouteService'; 
    }, 
});

應用程序/模板/file1.hbs

HTML


{{page-title "RouteService"}} 
<h1>Hello Geeks For Geeks</h1> 
<h3>Explaining RouteService rootURL properties</h3> 
{{outlet}}

輸出:輸出顯示基本 URL 為“/”

輸出1

示例 2:鍵入以下代碼以生成本示例的路由:

ember generate route file2

應用程序/配置/環境.js

Javascript


'use strict'; 
  
module.exports = function (environment) { 
    let ENV = { 
        modulePrefix: 'hello', 
        environment, 
  
        // Using /my-app/ as base address 
        rootURL: '/my-app/', 
        locationType: 'auto', 
        EmberENV: { 
            FEATURES: { 
  
                // Here you can enable experimental features  
                // on an ember canary build 
                // e.g. EMBER_NATIVE_DECORATOR_SUPPORT: true 
            }, 
            EXTEND_PROTOTYPES: { 
  
                // Prevent Ember Data from  
                // overriding Date.parse. 
                Date: false, 
            }, 
        }, 
  
        APP: { 
            // Here you can pass flags/options  
            // to your application instance 
            // when it is created 
        }, 
    }; 
  
    if (environment === 'development') { 
    } 
  
    if (environment === 'test') { 
  
        // Testem prefers this... 
        ENV.locationType = 'none'; 
  
        // keep test console output quieter 
        ENV.APP.LOG_ACTIVE_GENERATION = false; 
        ENV.APP.LOG_VIEW_LOOKUPS = false; 
  
        ENV.APP.rootElement = '#ember-testing'; 
        ENV.APP.autoboot = false; 
    } 
  
    if (environment === 'production') { 
        // here you can enable a  
        // production-specific feature 
    } 
  
    return ENV; 
};

應用程序/路線/file2.js

Javascript


import Route from '@ember/routing/route'; 
export default Route.extend({ 
    model() { 
        return 'RouteService'; 
    } 
}); 

應用程序/模板/file2.hbs

HTML


{{page-title "RouteService"}} 
<h1>Hello Geeks For Geeks</h1> 
<h3>Explaining RouteService rootURL properties</h3> 
{{outlet}}

輸出:

輸出2

參考:https://api.emberjs.com/ember/4.9/classes/RouterService/properties/rootURL?anchor=rootURL



相關用法


注:本文由純淨天空篩選整理自satyam00so大神的英文原創作品 Ember.js RouterService rootURL Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。