ionic2:启动屏幕期间的网络连接检查
我一直在努力找出如何在显示splashscreen时检查网络连接。我在许多地方搜索了代码,但大多数文章都过时了。 我遵循了这里提到的教程: 但后来我发现Network.connection已被弃用,并在ionic2网站上被Network.type取代。 所以我用Network.type everywhere替换了connection这个词。 因此,我查看了ionic2网站,找到了home.ts文件中包含的代码ionic2:启动屏幕期间的网络连接检查,ionic2,cordova-plugins,splash-screen,network-connection,ionic-native,Ionic2,Cordova Plugins,Splash Screen,Network Connection,Ionic Native,我一直在努力找出如何在显示splashscreen时检查网络连接。我在许多地方搜索了代码,但大多数文章都过时了。 我遵循了这里提到的教程: 但后来我发现Network.connection已被弃用,并在ionic2网站上被Network.type取代。 所以我用Network.type everywhere替换了connection这个词。 因此,我查看了ionic2网站,找到了home.ts文件中包含的代码 import {Network} from 'ionic-native';
import {Network} from 'ionic-native';
checkConnection() {
//console.log("entrou");
//console.log(Network);
let disconnectSubscription = Network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-( ')
});
disconnectSubscription.unsubscribe();
console.log("watch network");
console.log("Conexao" + Network.type);
let connectSubscription = Network.onConnect().subscribe(() => {
console.log('network connected!');
setTimeout(() => {
console.log('network status');
console.log(Network.type);
if (Network.type === 'WIFI') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
console.log("Sub" + connectSubscription);
connectSubscription.unsubscribe();
}
这是我的home.html文件
`<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-buttton (click)="checkConnection()">Check Network</button>
</ion-content>`
`
家
检查网络
`
我尝试实现相同的代码,但没有成功
我想知道我能用的确切代码是什么
如果这是正确的代码,我需要导入什么才能使用它
我还想知道如何在splashscreen期间运行它?
在控制台上,我发现了这些错误
本机:尝试调用Network.type,但未安装网络插件。
网络插件:“爱奥尼亚插件添加cordova插件网络信息”
但是我已经按照上面的命令安装了所需的插件。我还安装了“npm install ionic native”
我在看到此错误时重新安装了它们,但这种情况仍然存在
提供商
以检查网络状态,然后在app.component.ts中呼叫您的新提供商/服务
哦,不要注意消息:Native:尝试调用Network.type,但没有安装网络插件。
只需确保您已正确添加它:
ionic plugin add cordova plugin network information——在config.xml
中保存,添加以下内容:
constructor(private platform: Platform) {
platform.ready().then(() => {
// Check the network stuff here and do what you need to do
if (Network.type == 'WIFI') console.log('We are on WIFI!');
else console.log('We aren't on WIFI');
// then hide the splash screen manually
SplashScreen.hide();
});
}
这将使SplashScreen保持可见,直到您手动隐藏它
然后在app.component.ts
中执行以下操作:
constructor(private platform: Platform) {
platform.ready().then(() => {
// Check the network stuff here and do what you need to do
if (Network.type == 'WIFI') console.log('We are on WIFI!');
else console.log('We aren't on WIFI');
// then hide the splash screen manually
SplashScreen.hide();
});
}
本条的提述—
https://ionicframework.com/docs/native/network/
安装:
$ionic cordova插件添加cordova插件网络信息
$npm安装--save@ionic native/network
并将此代码放入app.component.ts
import { Network } from '@ionic-native/network';
constructor(private network: Network) { }
// watch network for a disconnect
let disconnectSubscription =
this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
// stop disconnect watch
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() =>
{
console.log('network connected!');
// We just got a connection but we need to wait briefly
// before we determine the connection type. Might need to wait.
// prior to doing any api requests as well.
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();
import { Network } from '@ionic-native/network';
...
@NgModule({
...
providers: [
...
Network
...
]
...
})
export class AppModule { }`
并将代码添加到app.module.ts
import { Network } from '@ionic-native/network';
constructor(private network: Network) { }
// watch network for a disconnect
let disconnectSubscription =
this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
// stop disconnect watch
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() =>
{
console.log('network connected!');
// We just got a connection but we need to wait briefly
// before we determine the connection type. Might need to wait.
// prior to doing any api requests as well.
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();
import { Network } from '@ionic-native/network';
...
@NgModule({
...
providers: [
...
Network
...
]
...
})
export class AppModule { }`
希望这会有所帮助。尝试爱奥尼亚插件添加cordova插件网络信息-保存
很抱歉,它不起作用。我仍然收到相同的错误。请查看代码一次。这是一个请求。您是否在设备中运行?不。我正在模拟器上运行。让我知道代码及其位置是否正确?我没有看到任何问题..除了maybe将其包装到this.platform.ready()。然后(()=>{})