NativeScript-Drop-Down icon indicating copy to clipboard operation
NativeScript-Drop-Down copied to clipboard

Could not load view for: DropDown.Error:

Open u14040426 opened this issue 10 years ago • 7 comments

Hi when I go to a page that has the DropDown element, I get this error. I am quite new to this, any help will be appreciated:

 EXCEPTION: Uncaught (in promise): Error: Error in pages/home/home.html:4:8 caused by: Could not load view for: DropDown.Error: com.tns.NativeScriptException: Failed to find module: "nativescript-drop-down/drop-down", relative to: app/tns_modules/
JS:     com.tns.Module.resolvePathHelper(Module.java:159)
JS:     com.tns.Module.resolvePath(Module.java:60)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1022)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:907)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:895)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:879)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:871)
JS:     com.tns.gen.org.nativescript.widgets.Async_CompleteCallback.onComplete(org.nativescript.widgets.Async$CompleteCallback.java)
JS:     org.nativescript.widgets.Async$Http$HttpRequestTask.onPostExecute(Async.java:465)
JS:     org.nativescript.widgets.Async$Http$HttpRequestTask.onPostExecute(Async.java:379)
JS:     android.os.AsyncTask.finish(AsyncTask.java:651)
JS:     android.os.AsyncTask.-wrap1(AsyncTask.java)
JS:     android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
JS:     android.os.Handler.dispatchMessage(Handler.java:102)
JS:     android.os.Looper.loop(Looper.java:148)
JS:     android.app.ActivityThread.main(ActivityThread.java:5417)
JS:     java.lang.reflect.Method.invoke(Native Method)
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Home.html:

<StackLayout>
    <GridLayout rows="auto, auto, *" columns="auto, *">
        <TextField hint="Height" keyboardType="text" autocorrect="false" autocapitalizationType="none" [(ngModel)]="height" class="input-text-box" row="0" colSpan="1"></TextField>
        <TextField hint="Age"  keyboardType="text" autocorrect="false" autocapitalizationType="none" [(ngModel)]="age" class="input-text-box" row="0" colSpan="1"></TextField>
        <DropDown #dd backroundColor="red" [items]="items" [(ngModel)]="index" row="0" colSpan="2"></DropDown>
    </GridLayout>
</StackLayout>

Home.component.ts

import {Component, OnInit} from "@angular/core";

import {Http} from "@angular/http";
import {UserService} from "../../service/UserService/user.service";
import {Page} from "ui/page";
import {DropDown} from "nativescript-drop-down/drop-down"

@Component({
    selector: "home",
    templateUrl: "pages/home/home.html",
    styleUrls: ["pages/home/home.common.css", "pages/home/home.css"]
})
export class HomeComponent implements OnInit{
    private items: Array<any> = ["123455","abcdef","!@#$%", "}{POIU"];
    private index:number;
    private height: number;
    private age: number;

    constructor(private authHttp: Http, private userService: UserService, private page: Page) {
    }
    
    ngOnInit(){
        this.page.actionBarHidden = true;
    }
    
}

u14040426 avatar Nov 22 '16 08:11 u14040426

Hey @u14040426 , have you registered the control as explained in the Angular2 example in the readme https://github.com/PeterStaev/NativeScript-Drop-Down#angular-2-example ?

Also seems your import is wrong and it should be changed to:

import {DropDown} from "nativescript-drop-down"

PeterStaev avatar Nov 22 '16 08:11 PeterStaev

I did do it this way first

import {registerElement} from "nativescript-angular/element-registry";
registerElement("DropDown", () => require("nativescript-drop-down/drop-down").DropDown);

but it gives me the same error.

u14040426 avatar Nov 22 '16 08:11 u14040426

Note that this code should go in your main.ts file (where you do your bootstrapping) and NOT in the specific component TS file.

PeterStaev avatar Nov 22 '16 08:11 PeterStaev

Yes I did that initially and I got the same error. Then I tried the other method above, same result.

u14040426 avatar Nov 22 '16 08:11 u14040426

Hey @u14040426 , I've just tried with the code below and it works fine, take a look and see what you are doing wrong in your code.

// main.ts
import { platformNativeScriptDynamic, NativeScriptModule } from "nativescript-angular/platform";
import { NgModule } from "@angular/core";
import { AppComponent } from "./app.component";
import { registerElement } from "nativescript-angular/element-registry";

registerElement("DropDown", () => require("nativescript-drop-down/drop-down").DropDown);

@NgModule({
    declarations: [AppComponent],
    bootstrap: [AppComponent],
    imports: [NativeScriptModule],
})
class AppComponentModule {}

platformNativeScriptDynamic().bootstrapModule(AppComponentModule);
// app.component.ts
import {Component} from "@angular/core";
import { DropDown } from "nativescript-drop-down";

@Component({
    selector: "my-app",
    templateUrl: "app.component.html",
})
export class AppComponent {
    public selectedIndex = 1;
    public items: Array<string>;

    constructor() {
        this.items = [];
        for (var i = 0; i < 5; i++) {
            this.items.push("data item " + i);
        }
    }

    public onchange(selectedi){
        console.log("selected index " + selectedi);
    }
}
<!-- app.component.html -->
<StackLayout>
    <GridLayout rows="auto, auto, *" columns="auto, *">
        <DropDown #dd backroundColor="red" [items]="items" [selectedIndex]="selectedIndex" (selectedIndexChange)="onchange(dd.selectedIndex)" row="0" colSpan="2"></DropDown>
        <Label text="Selected Index:" row="1" col="0" fontSize="18" verticalAlignment="bottom"></Label>
        <TextField [text]="selected" row="1" col="1" ></TextField>
    </GridLayout>
</StackLayout>

PeterStaev avatar Nov 22 '16 09:11 PeterStaev

I get this error when I try to import it in my component. app/pages/home/home.component.ts(6,24): error TS2307: Cannot find module 'nativescript-drop-down'.

u14040426 avatar Nov 22 '16 11:11 u14040426

Okay I found this issue #38 that helped with the module not found issue.

u14040426 avatar Nov 22 '16 11:11 u14040426