【问题标题】:How can I migrate a web app with Angular v7 to mobile NativeScript app如何将使用 Angular v7 的 Web 应用程序迁移到移动 NativeScript 应用程序
【发布时间】:2019-05-20 09:56:23
【问题描述】:

我使用 Angular v7 制作了一个简单的 Web 应用程序,我想将它迁移到 NativeScript 移动应用程序。

我按照https://docs.nativescript.org/angular/code-sharing/migrating-a-web-project#migrating-components 中的说明进行操作,但遇到了困难。

该应用程序正在我的 Android 手机中运行,但它不显示内容,因为我需要针对移动应用程序对其进行适当调整。它显示自动生成的作品。问题是我不知道如何正确调整代码,因为我找不到正确的文档。有什么帮助吗?

我在 app.component.html 中的代码:

<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>
<weather-search></weather-search>
<weather-list></weather-list>

<router-outlet></router-outlet>

我在weather-search.component.html中的代码:

<section class="weather-search">
    <form (ngSubmit)="onSubmit(f)" #f="ngForm">
        <div class="test_up">
            <label for="city" class="city">City</label>
            <input name="location" type="text" id="city" ngModel required>
        </div>
        <div class="test_down">
            <label for="countryCode" class="city">Country Code</label>
            <input name="countryCode" type="text" id="code" ngModel required>
        </div>
        <button type="submit">Add City</button>
    </form>
    <button (click)="onClickLocationData(f)">Get geolocation</button>
    <label for="latitude" class="map" *ngIf="latitude!=0">Latitude: {{latitude}} </label>
    <label for="longitude" class="map" *ngIf="longitude!=0">Longitude: {{longitude}} </label>
    <div id="map" class="mapLocation"></div>
</section>

我在weather-list.component.html中的代码:

<section class="weather-list">
    <weather-item *ngFor="let weatherItem of weatherItems" [item]="weatherItem"></weather-item>
</section>

我在weather-item.component.html中的代码:

<article class="weather-element">
    <div class="col-1"> 
        <h3>{{ weatherItem.cityName }}</h3>
        <p class="info">{{ weatherItem.description }}</p>
    </div>
    <div class="col-2"> 
        <span class="temperature">{{ weatherItem.temperature }}°C</span>
    </div>
</article>

【问题讨论】:

  • The problem is I do not know how I can do adjust properly the code => 有什么可以帮到你的,让我知道?我最近参与了几个 {NS} 代码共享项目。
  • @Narendra 嗨!感谢您及时的回复。成功运行命令 ng add @nativescript/schematics 和 **ng g migrate-component --name= ** 为每个组件,如果我在我的移动设备上运行它,它可以工作,但它确实不显示适当的内容。现在我不应该调整我发布的每个文件的内容吗?如何?你能用我的代码举个例子吗?
  • @Narendra 嗨!你能告诉我如何在 weather-search.component.html 中调整我的表单吗?
  • @Narendra 嘿,你按照你说的在 GitHub 中创建了一个 repo 吗?

标签: android angular migration nativescript angular7


【解决方案1】:

正如该文档所建议的那样,您必须创建 component.tns.html 文件,因为其他 HTML 元素不适用于移动设备。

在您的 .tns.html 文件中,您可以替换为 StackLayout 或 GridLayout。您可以参考https://www.nslayouts.com/ 了解有关 {NS} 布局的更多信息。

在你的weather-list.component.tns.html:

<StackLayout class="weather-list">
    <weather-item *ngFor="let weatherItem of weatherItems" [item]="weatherItem"></weather-item>
</StackLayout >

在weather-item.component.tns.html

中
<StackLayout class="weather-element">
        <StackLayout>
            <Label text="{{ weatherItem.cityName }}"></Label>
            <Label text="{{ weatherItem.description }}"></Label>
        </StackLayout>
        <StackLayout>
            <Label text="{{ weatherItem.temperature }}"></Label>
        </StackLayout>
    </StackLayout>

【讨论】:

  • weather-list.component.html中的这样的自制组件呢?你可以给我一个例子吗?谢谢!!
  • 我已经为您添加了一个示例作为答案。您也可以在play.nativescript.org 创建示例
  • 非常感谢!如果你有其他的迁移来源,如果你能分享一下就太好了!!
  • 好的,我会上传一个 git hub 示例并很快分享!!
  • 您可以创建一个聊天室以便我分享我的问题吗?我调整了您向我展示的所有代码,但它再次显示自动生成的作品!
猜你喜欢
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2021-06-20
  • 2011-10-08
相关资源
最近更新 更多