Hi,
I’m pretty new to elm and front-end. And I encountered the same problem which has been solved in this previous post 4088, but it didnt work for me.
edit: Neither worked on github.io
, here is my github repository, in case you want to check it.
I’m using vscode’s live server extention to host in local. And I have tried to set the proxy of it based on that post:
# settings.json
{
"liveServer.settings.proxy": {
"enable": true,
"baseUri": "/index.html",
"proxyUri": "http://localhost:5500/index.html?"
}
}
And also set it in varies way. It never work.
The elm code I wrote is the guide code:
-- Main.elm
module Main exposing (main)
import Html exposing (..)
import Html.Attributes exposing (class, src, alt, href)
import Browser
import Url
import Browser.Navigation as Nav
-- MAIN
main : Program () Model Msg
main = Browser.application
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
, onUrlChange = UrlChanged
, onUrlRequest = LinkClicked
}
-- MODEL
type alias Model =
{ key : Nav.Key
, url : Url.Url
}
init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init _ url key = ( Model key url, Cmd.none )
-- UPDATE
type Msg
= LinkClicked Browser.UrlRequest
| UrlChanged Url.Url
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.key (Url.toString url) )
Browser.External href ->
( model, Nav.load href )
UrlChanged url ->
( { model | url = url }
, Cmd.none
)
-- VIEW
view : Model -> Browser.Document Msg
view model =
{ title = "URL Interceptor"
, body =
[ text "The current URL is: "
, b [] [ text (Url.toString model.url) ]
, ul []
[ viewLink "/home"
, viewLink "/profile"
, viewLink "/reviews/the-century-of-the-self"
, viewLink "/reviews/public-opinion"
, viewLink "/reviews/shah-of-shahs"
]
]
}
viewLink : String -> Html msg
viewLink path =
li [] [ a [ href path ] [ text path ] ]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions _ =
Sub.none
Then compile it use elm make
and uglifyjs
to main.js
, which linked in index.html
:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="main.js"></script>
<link rel="stylesheet" href="./assets/style.css">
<link rel="icon" href="assets/logo.svg">
</head>
<body>
<script>var app = Elm.Main.init();</script>
</body>
</html>
Is there any information I need to offer? Just mentioned below.
Please help me,
LTstrange.