2017-08-01 81 views
0

我是新的反應本地和typescript.I想創建權限模塊爲我的反應本地的項目,我分隔在下面Yonah's project兩個鏈接和Facebook's permissions project如何在反應原生項目上導入權限模塊?

Additionaly

NPM安裝,NPM啓動,紗運行構建 - 觀察和反應 - 本地運行 - 安卓

命令運行順利,但權限模塊不使用它,我不提出這個模塊添加我的項目。
我在下面嘗試鏈接;

const permission = require('react-native-permissions') 
import React from 'react' 
import { 
StyleSheet, 
TouchableHighlight, 
Text, 
View, 
Alert, 
AppState, 
Platform, 
PermissionsAndroid, 
} from 'react-native' 

interface Props { 
names: string[] 
} 

interface PermissionsState { 
status: PermissionStatus 
} 

type PermissionStatus = 'granted' | 'denied' | 'never_ask_again' | '' 

export class permissions extends React.Component<Props, PermissionsState> 
{ 
constructor(props: Props, context: any) 
// tslint:disable-next-line:brace-style 
{ 
    super(props, context) 

    this.state = { 
     status: '', 
    } 
} 

public render() 
// tslint:disable-next-line:brace-style 
{ 
    return (<View> a </View>) 
} 
} 

async function requestCameraPermission() { 
try { 
    const granted = await PermissionsAndroid.request(
     PermissionsAndroid.PERMISSIONS.CAMERA, 

    ) 
    if (granted === PermissionsAndroid.RESULTS.GRANTED) { 
     // console.log("kamerayı kullanabilirsiniz") 
    } else { 
     // console.log('kamerayı kullanamazsınız') 
    } 
    } catch (err) { 
    // console.warn(err) 
    } 
    } 

    export default permission 

回答

0

requestCameraPermission函數未被調用。你可以嘗試調用函數

constructor(props: Props, context: any) 
// tslint:disable-next-line:brace-style 
{ 
    super(props, context) 

    this.requestCameraPermission= this.requestCameraPermission.bind(this); 

    this.state = { 
     status: '', 
    } 
} 

componentWillMount() { 
    this.requestCameraPermission(); 
    ..... 
} 

的多個請求看到這個例子

export async function requestForPermission() { 
try{ 

const granted = await PermissionsAndroid.requestMultiple(
    [ 
    PermissionsAndroid.PERMISSIONS.CAMERA, 
    PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, 

    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, 
    PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION, 

    PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, 
    PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, 
    ], 

) 

........ 

} catch (err) { 
    console.warn(err) 
} 
} 

@ ++

+0

componentWillMount得到一個錯誤,這是[TS]找不到名稱 'componentWillMount'。 任何 –

+0

和我需要這種方法,構造函數是相同的行爲,只要我知道 –

+0

我編輯我的示例! – TrinitA