官术网_书友最值得收藏!

  • Ionic Cookbook
  • Hoc Phan
  • 548字
  • 2021-07-09 21:15:11

Viewing the app using your web browser

In order to "run" the web app, you need to turn your /www folder into a web server. Again there are many methods to do this and people tend to stick with one or two ways to keep things simple. A few other options are unreliable such as Sublime Text's live watch package or static page generator (for example, Jekyll, Middleman App, and so on). They are slow to detect changes and may freeze your IDE so these won't be mentioned here.

Getting ready

The recommended method is to use the ionic serve command line. It basically launches an HTTP server so you can open your app in a desktop browser.

How to do it…

  1. First you need to be in the project folder. Let's assume it is the Side Menu HelloWorld:
    $ cd HelloWorld_Sidemenu
    
  2. From there, just issue the simple command line:
    $ ionic serve
    

That's it! There is no need to go into the /www folder or figure out which port to use. The command line will provide these options while the web server is running:

How to do it…

The most common option to use here is r to restart or q to quit when you are done.

There is an additional step to view the app with the correct device resolution:

  1. Install Google Chrome if it's not already on your computer.
  2. Open the link (for example, http://localhost:8100/#/app/playlists) from ionic serve in Google Chrome.
  3. Turn on Developer Tools. For example, in Mac's Google Chrome, select View | Developer | Developer Tools:
    How to do it…
  4. Click on the small mobile icon in the Chrome Developer Tools area:
    How to do it…
  5. There will be a long list of devices to pick from:
    How to do it…
  6. After selecting a device, you need to refresh the page to ensure the UI is updated. Chrome should give you the exact view resolution of the device.
    How to do it…

Most developers would prefer to use this method to code as you can debug the app using Chrome Developer Tools. It works exactly like any web application. You can create breakpoints or output variables to the console.

How it works...

Note that ionic serve is actually watching everything under the /www folder except the JavaScript modules in the /lib folder. This makes sense because there is no need for the system to scan through every single file when the probability for it to change is very small. People don't code directly in the /lib folder but only update when there is a new version of Ionic. However, there is some flexibility to change this.

You can specify a watchPatterns property in the ionic.project file located in your project root to watch (or not watch) for specific changes:

{
  "name": "myApp",
  "app_id": "",
  "watchPatterns": [
    "www/**/*",
    "!www/css/**/*",
    "your_folder_here/**/*"  
  ]
}

While the web server is running, you can go back to the IDE and continue coding. For example, let's open the playlists.html file under /www/templates and change the first line to this:

<ion-view view-title="Updated Playlists">

Go back to the web browser where Ionic opened the new page; the app interface will change the title bar right away without requiring you to refresh the browser. This is a very nice feature when there is a lot of back and between code changes and allows checking on how it works or looks in the app instantly.

主站蜘蛛池模板: 昔阳县| 漠河县| 九龙坡区| 公主岭市| 澄迈县| 孟连| 武陟县| 陇西县| 阿城市| 万全县| 保亭| 铁岭市| 邵阳市| 东兴市| 尼勒克县| 莎车县| 杨浦区| 开化县| 苏尼特右旗| 措美县| 榆林市| 乐业县| 龙江县| 侯马市| 宜宾县| 金坛市| 平果县| 东乡族自治县| 九龙坡区| 河东区| 义乌市| 宣威市| 色达县| 广平县| 怀远县| 闽清县| 鹤岗市| 邵阳县| 青铜峡市| 延吉市| 武清区|